2012-12-15 88 views
0

可能重複:
JQuery: How to select rows from a table如何使用jQuery從HTML表中選擇特定的行?

我要選擇基於使用jQuery一定條件下表中的特定行。例如,如果一個表有25行並帶有一個複選框列來檢查每一行,我想指定一個條件,並且如果該條件爲真,那麼只應該選中該複選框,例如,5行滿足該條件,所以只有應該檢查5行。該選擇可能依賴於表格的其他列。我如何用jQuery實現這一點?

回答

1

你需要這樣的事情,

var chkbox = $('#yourTableId tr').eq(indexOfRowYouWant).find(':checkbox').attr('checked', true); 
1
$('tr', table).each(function() { 

    this // refers to the current TR element 

    // check if the condition is met for this row 
    var conditionMet = ... 

    // and set the checked state accordingly 
    $('.checkbox', this)[0].checked = conditionMet; 

}); 

其中table是你的表元素的引用,並"checkbox"是類每行內的<input type=checkbox>元素。

相關問題