开发者

Jquery - Select td value for all the tr with a class

开发者 https://www.devze.com 2023-03-06 03:29 出处:网络
I have a table <table> <tr class=\"PO1\"></tr> <tr class=\"PO2\"></tr> <tr class=\"PO3\"></tr>

I have a table

<table>
       <tr class="PO1"></tr>
       <tr class="PO2"></tr>
       <tr class="PO3"></tr>
</table>

How can I loop through all tr with class "PO1" and get the value of each 'td' value?

$("t开发者_JS百科able#id tr .PO1").each(function(i)
{
    // how to get the td values??
});


var values = $('table tr.PO1 td').map(function(_, td) {
    return $(td).text();
}).get();

This would just create an array with the text contents from each td. Probably a better idea to use a map/object instead:

var values = $('table tr.PO1 td').map(function(index, td) {
    var ret = { };

    ret[ index ] = $(td).text();
    return ret;
}).get();


The space before the .P01 is what's breaking your current code.

$("tr.PO1 td").each(function(i){
    $(this).text()
});


notice : I removed a space before .PO1 because your tr has class P01

$("table#id tr.PO1").each(function(i)
{
    $(this).find("td").innerHTMl() //for example
});


$("table#id tr.PO1").each(function(i)
{
    i.children('td').each(function(tdEL) {
        // tdEl.val();
    });
});

Notice the space I removed between tr and .PO1. In you case it will try to find each tr with an child having the class .PO1.

0

精彩评论

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