How to catch the td value in jquery on mouse click event? I have done these things in pure js but it is lengthy. Can jquery have easy solution? I want to add those ca开发者_如何学JAVAtch value in the form text field.
$("#theTable").click(function(e) {
var data = $(e.target).closest("td").text();
});
<script type="text/javascript">
$(document).ready(function(){
$("#myTable td").click(function(){
alert($(this).html());
})
})
</script>
<table id="myTable" border="1" >
<tr>
<td>value 1</td>
<td>value 2</td>
<td>value 3</td>
</tr>
</table>
You can use .delegate()
. One handler on the container element (table) manages events on all the contained elements (filtered to your preference, e.g. "td"):
$('#thetable').delegate('td','click', function(){
alert('Value is ' + $(this).text() );
});
精彩评论