简体   繁体   English

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

[英]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.

相关问题 如何使用绝对行号从复选框中使用jQuery获取表单元格的值 - How to get value of table cell with jquery from checkbox using absolute row number jQuery,如何获取表行中特定复选框的值? - jQuery, how to get value of a specific checkbox in table row? 通过TD类和jQuery中单元格中的特定值从表中获取行 - Get row from a table by td class and specific value in the cell in jquery 如何使用jQuery从具有特定类的单行中获取表单元格的内容 - How to get the contents of a table cell from a single row with a specific class using jquery 如何使用 jQuery 从 HTML 表格的倒数第二行获取单元格值 - How to get the cell value from an HTML table's second last row using jQuery 使用jquery获取表的特定行的值 - get value of a specific row of a table using jquery 如何从行中的下拉列表中单击的jQuery数据表中的单元格获取值 - How do i get a value from a cell in jquery datatables clicking on dropdown inside a row 如何使用jQuery从HTML表获取复选框和文本框值 - How to get checkbox and textbox value from HTML table using jquery 如何使用jQuery将特定单元格附加到外部表中的特定行 - How do I append a specific cell to a specific row in the outer table with jQuery 如何使用jquery遍历表以选择每一行上的特定表单元格? - How do loop through a table to select a specific table cell on each row using jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM