开发者

how to catch the table data td value?

开发者 https://www.devze.com 2023-01-30 15:59 出处:网络
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

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() );
});
0

精彩评论

暂无评论...
验证码 换一张
取 消