jQuery - Click event on elements with in a table and getting element values

后端 未结 7 1158
说谎
说谎 2020-12-25 10:48

I have the following HTML in a JSP file:

相关标签:
7条回答
  • 2020-12-25 11:15

    Unless otherwise definied (<tfoot>, <thead>), browsers put <tr> implicitly in a <tbody>.

    You need to put a > tbody in between > table and > tr:

    $("div.custList > table > tbody > tr")
    

    Alternatively, you can also be less strict in selecting the rows (the > denotes the immediate child):

    $("div.custList table tr")
    

    That said, you can get the immediate <td> children there by $(this).children('td').

    0 讨论(0)
  • 2020-12-25 11:15
    <script>
    jQuery(document).ready(function() {
        jQuery("tr").click(function(){
           alert("Click! "+ jQuery(this).find('td').html());
        });
    });
    </script>
    
    0 讨论(0)
  • 2020-12-25 11:26

    Since TR elements wrap the TD elements, what you're actually clicking is the TD (it then bubbles up to the TR) so you can simplify your selector. Getting the values is easier this way too, the clicked TD is this, the TR that wraps it is this.parent

    Change your javascript code to the following:

    $(document).ready(function() {
        $(".dataGrid td").click(function() {
            alert("You clicked my <td>!" + $(this).html() + 
                  "My TR is:" + $(this).parent("tr").html());
            //get <td> element values here!!??
        });
    });​
    
    0 讨论(0)
  • 2020-12-25 11:31
    $("body").on("click", "#tableid tr", function () {
        debugger
        alert($(this).text());
    });
    
    $("body").on("click", "#tableid td", function () {
        debugger
        alert($(this).text());
    });
    
    0 讨论(0)
  • 2020-12-25 11:31

    $(this).find('td') will give you an array of td's in the tr.

    0 讨论(0)
  • 2020-12-25 11:38

    This work for me!

    $(document).ready(function() {
        $(document).on("click", "#tableId tbody tr", function() {
            //some think
        });
    });
    
    0 讨论(0)
提交回复
热议问题