从同一个表中读取tr id的td值

时间:2014-11-05 13:04:47

标签: javascript jquery row

我正在尝试根据tr id读取表的值,但无法解决如何执行此操作。

    // Id of the tr in question for example.row_17 
    var d =c["id"]; 
    console.log(d); 

    // Here I can read all the td values, but I only want
    // the ones inside the tr id = "row_17" 

    var cols =document.getElementById('report_table').getElementsByTagName('td'), 

     colslen = cols.length, i = -1; > while(++i < colslen)
    {  console.log(cols[i].innerHTML); 

}

2 个答案:

答案 0 :(得分:1)

由于您使用jQuery标记它,您可以通过执行以下操作来执行此操作:

var id = c["id"];

// Select only the row with id specified in 'id' and loop through all 'td's' of that row.
$("#" + id).find("td").each(function()
{
    // Log the html content of each 'td'.
    console.log($(this).html());
});

答案 1 :(得分:1)

万一你想要一个只有JavaScript的解决方案(没有jQuery):

var id = c["id"];

var tds = document.querySelectorAll('#' + id + ' td');
for(var i = 0; i < tds.length; i++) {
    console.log(tds[i].innerHTML);
}

Demo