2014-11-05 95 views
0

我想讀取基於tr id的表的值,但無法將我的頭圍繞如何做到這一點。我希望有人會如此善良並幫助我。謝謝。從同一表tr ID讀取td值

  // Id of the tr in quetion 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); 

} 

回答

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()); 
}); 
+0

是的,這很適合我。非常感謝你。我會盡快接受答案。 – user1054844 2014-11-05 13:14:39

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