Get second td of tr using jquery

后端 未结 5 2118
攒了一身酷
攒了一身酷 2021-02-12 10:54

I\'m trying to get td values of tr.. but unfortunately it\'s not working.. may be there is something wrong

My html looks like

         


        
相关标签:
5条回答
  • 2021-02-12 11:10

    Replace n with child no.

    $(".dname td:nth-child(n)").text();
    

    For e.g. for 2nd child

    $(".dname td:nth-child(2)").text();
    
    0 讨论(0)
  • 2021-02-12 11:11

    jQuery find() method returns the descendants of the selected element. You are already selecting the <tr> with a class of dname and then trying to find a descendant which is also a <tr>.

    https://api.jquery.com/find/

    The following should work:

    jQuery(".dname").find("td:eq(1)").text();
    

    Edit: text() instead of val() as @freedomn-m pointed out

    0 讨论(0)
  • 2021-02-12 11:13

    You're already filtering on the tr by its class name, so there's no need to find by tr again.

    jQuery(".dname").find("td:eq(1)").text()
    

    Also, you need to .text() to get the contents of a <td> not .val().

    JSBin here.

    Hope this helps :)

    0 讨论(0)
  • 2021-02-12 11:13

    Because that <td> is the last child of your <tr> you can access it like below,

    jQuery(".dname > td:last-child").text();

    0 讨论(0)
  • 2021-02-12 11:22

    Just wanted to point out that if you want some particular value inserted in the cell then do the following (for example if you need to show the text TEST in the table cell):

    $(".dname td:nth-child(n)").text("TEST");

    0 讨论(0)
提交回复
热议问题