![](/img/trans.png)
[英]How to get value of table cell with jquery from checkbox using absolute row number
[英]How do I get the checkbox value from a specific cell in a table row using jQuery?
我有一個表,每行包含多個復選框。 我需要獲取每個復選框的值。 我已經能夠執行以下操作來檢索表格第一列中復選框的值...
$('#myTable tbody tr td:first-child input:checkbox').each(function() {
if (this.checked) {
//Do something
}
});
但現在我需要迭代每一行,並獲取每個復選框的值。 這就是我一直在嘗試的特定復選框......
if($(this).find(':input[type="checkbox"]').eq(0).attr('checked')) {
myVariable = 'so and so';
}
else {
myVariable = 'something else';
}
哪個不起作用。 關於如何讓這個工作的任何建議?
你的問題並不是那么清楚,但是從你的第二個例子開始,我認為'this'是tr。 下面顯示了如何獲取行內的第一個復選框並測試是否已選中
if ( $(this).find('input:checkbox:first').is(':checked') ) {...}
迭代行中的所有復選框
$(this).find('input:checkbox').each( function(){...}
迭代行中所有選中的復選框
$(this).find('input:checkbox:checked').each( function(){...}
為什么不從選擇器中刪除td:first-child
?
$('#myTable tbody tr input:checkbox').each(function() {
if (this.checked) {
//Do something
}
});
或者,如果您想以某種方式對每行的復選框進行分組:
$('#myTable tbody tr').each(function() {
$(this).find('input:checkbox:checked').each(function() {
//..
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.