[英]How do I get the checkbox value from a specific cell in a table row using jQuery?
I have a table that contains multiple checkboxes in each row. 我有一个表,每行包含多个复选框。 I need to get the value of each individual checkbox.
我需要获取每个复选框的值。 I've been able to do something like the following to retrieve the value of a checkbox in the first column of a table...
我已经能够执行以下操作来检索表格第一列中复选框的值...
$('#myTable tbody tr td:first-child input:checkbox').each(function() {
if (this.checked) {
//Do something
}
});
But now I need to iterate over each row, and get the value of each individual checkbox. 但现在我需要迭代每一行,并获取每个复选框的值。 This is what I've been trying for a specific checkbox...
这就是我一直在尝试的特定复选框......
if($(this).find(':input[type="checkbox"]').eq(0).attr('checked')) {
myVariable = 'so and so';
}
else {
myVariable = 'something else';
}
Which doesn't work. 哪个不起作用。 Any suggestions on how I can get this working?
关于如何让这个工作的任何建议?
Your question is not that clear however going from your second example I assume the 'this' is the tr. 你的问题并不是那么清楚,但是从你的第二个例子开始,我认为'this'是tr。 The below shows how to get the first checkbox inside the row and test if it is checked
下面显示了如何获取行内的第一个复选框并测试是否已选中
if ( $(this).find('input:checkbox:first').is(':checked') ) {...}
To iterate all the checkboxes in the row 迭代行中的所有复选框
$(this).find('input:checkbox').each( function(){...}
To iterate all the checked checkboxes in the row 迭代行中所有选中的复选框
$(this).find('input:checkbox:checked').each( function(){...}
Why not remove td:first-child
from your selector? 为什么不从选择器中删除
td:first-child
?
$('#myTable tbody tr input:checkbox').each(function() {
if (this.checked) {
//Do something
}
});
Or if you want to group the checkboxes per row somehow: 或者,如果您想以某种方式对每行的复选框进行分组:
$('#myTable tbody tr').each(function() {
$(this).find('input:checkbox:checked').each(function() {
//..
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.