如何使用 jQuery 从表行中的特定单元格获取复选框值?

发布于 2024-09-11 01:17:11 字数 530 浏览 0 评论 0原文

我有一个表,每行包含多个复选框。我需要获取每个复选框的值。我已经能够执行类似以下操作来检索表第一列中复选框的值...

$('#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

说好的呢 2024-09-18 01:17:11

你的问题不太清楚,但是从你的第二个例子来看,我假设“this”是 tr。下面显示了如何获取行内的第一个复选框并测试它是否被选中

if ( $(this).find('input:checkbox:first').is(':checked') ) {...}

迭代该行中的所有复选框

$(this).find('input:checkbox').each( function(){...}

迭代该行中所有选中的复选框

   $(this).find('input:checkbox:checked').each( function(){...}

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

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(){...}
活泼老夫 2024-09-18 01:17:11

为什么不从选择器中删除 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() {
        //..
    });
});

Why not remove td:first-child from your selector?

$('#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() {
        //..
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文