简体   繁体   English

如何使用jQuery获取表中每一行的复选框状态?

[英]How to get checkbox state for each row in table using jQuery?

I am trying to get the state of a checkbox in each row within a table, I'm succesfully retrieving one of the values of the columns from the table, but when it comes to the column that holds the checkboxes elements, I cannot get the state. 我正在尝试获取表中每一行中复选框的状态,我正在从表中成功检索列的值之一,但是当涉及到包含复选框元素的列时,我无法获取州。

Here is the code I am using to iterate through the table for those rows selected: 这是我用来遍历表中所选行的代码:

$.each($("input[name='query']:checked"), function () {
    var data = $(this).parents('tr:eq(0)');

    var Name = $(data).find('td:eq(1)').text();
    var Value = $(data).find('td:eq(2)'); //This is the checkbox column

    console.log('Name: ' + Name + ' - Value: ' + Value );
});

And here is one sceenshot of the table itself: 这是桌子本身的一个画面:

在此处输入图片说明

The catch here is, if the users selects a row (ticks the checkbox in the first column) and then tries to save the changes, I need to get the value in the Name column and then the state of the checkbox in the Value column. 这里的问题是,如果用户选择一行(选中第一列中的复选框),然后尝试保存更改,则需要在“ 名称”列中获取值,然后在“ 值”列中获取复选框的状态。

Finally, here is what the console is printing using the code mentioned above: 最后,这是控制台使用上述代码打印的内容:

在此处输入图片说明

Change 更改

var Value = $(data).find('td:eq(2)');

to

var Value = $(data).find('td:eq(2) input').prop("checked");

or 要么

var Value = $(data).find('td:eq(2) input').is( ":checked" )

You have to find checkbox in column and check for checked property 您必须在列中找到checkbox并检查checked属性

Change 更改

var Value = $(data).find('td:eq(2)');

to

var Value = $(data).find('td:eq(2)').find("input[type='checkbox']").prop("checked");

or 要么

var Value = $(data).find('td:eq(2) input').prop("checked");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在jQuery中,如何获取使用复选框选择的表的特定行的值? - In jquery how to get the values of a particular row of a table that is selected using checkbox? 如何使用jQuery获取每个选中的复选框 - How to get each checked checkbox using jQuery 使用 Jquery 根据 Checkbox ID 在表的每一行中选中、取消选中复选框 - Check, unckeck checkbox in each row of table based on Checkbox ID using Jquery 获取表格每一行的复选框总和 - Get the Sum of checked checkbox for each row of table 如何使用绝对行号从复选框中使用jQuery获取表单元格的值 - How to get value of table cell with jquery from checkbox using absolute row number 如何使用javascript或jquery获取在表的特定行中存在的复选框和文本框值 - how to get checkbox and textbox value present in a particular row of a table using javascript or jquery 如何使用jQuery基于复选框状态获取隐藏字段的值 - How to get value of a hidden field based on a checkbox state using jquery 使用jquery获取表格第一行每列单元格的宽度 - Get width of each column cell of first row of a table using jquery 是否可以使用Jquery的每个函数获取表行的父级和子级? - Is it possible to get the parent and child of table row using each function of Jquery? 使用JQuery获取复选框选中状态 - Get checkbox checked state using JQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM