如何使用 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';
}
这不起作用。关于如何让它发挥作用有什么建议吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的问题不太清楚,但是从你的第二个例子来看,我假设“this”是 tr。下面显示了如何获取行内的第一个复选框并测试它是否被选中
迭代该行中的所有复选框
迭代该行中所有选中的复选框
Your question is not that clear however going from your second example I assume the 'this' is the tr. The below shows how to get the first checkbox inside the row and test if it is checked
To iterate all the checkboxes in the row
To iterate all the checked checkboxes in the row
为什么不从选择器中删除
td:first-child
呢?或者,如果您想以某种方式对每行的复选框进行分组:
Why not remove
td:first-child
from your selector?Or if you want to group the checkboxes per row somehow: