jQuery - Click event on elements with in a ta

2020-02-18 05:03发布

I have the following HTML in a JSP file:

<div class="custList">
   <table class="dataGrid">
      <c:forEach var="cust" items="${custList}">
         <tr>
            <td>${cust.number}</td>
            <td>${cust.description}</td>
            <td>${cust.type}</td>
            <td>${cust.status}</td>
        </tr>
     </c:forEach>
  </table>
</div>

I need to be able to trigger a 'click' event on each of the dynamically created <tr> tags and also be able to access the values of the <td> tags (of the clicked <tr>) from within the JavaScript function. I have this function already, but sadly it doesn't seem to be working.

$(document).ready(function() {
    $("div.custList > table > tr").live('click', function() {
        alert("You clicked my <tr>!");
        //get <td> element values here!!??
    });
});

Update (Jan 2016): jQuery.live is deprecated (as noted here:http://api.jquery.com/live/)

As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers.

7条回答
Anthone
2楼-- · 2020-02-18 06:01
<script>
jQuery(document).ready(function() {
    jQuery("tr").click(function(){
       alert("Click! "+ jQuery(this).find('td').html());
    });
});
</script>
查看更多
登录 后发表回答