如何从 td(表),JavaScript 中获取 CheckBox 的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30488838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:15:16  来源:igfitidea点击:

How to get a value of a CheckBox from td (table), JavaScript?

javascriptjqueryhtmlcheckbox

提问by Slavik Ostapenko

I have a table with text data but in one cell I have a checkbox. I can extract data from td, but I can't figure out how to extract the checkbox value

我有一个包含文本数据的表格,但在一个单元格中我有一个复选框。我可以从 td 中提取数据,但我不知道如何提取复选框值

Here's what I've tried (see attached picture!)

这是我尝试过的(见附图!)

$('#tableData tbody tr:eq('+row+')').find('td:eq(8)').eq(0).value

https://drive.google.com/file/d/0BxKM9sqBGY28cXp5ZzVCc0dRc28/view?usp=sharing

https://drive.google.com/file/d/0BxKM9sqBGY28cXp5ZzVCc0dRc28/view?usp=sharing

回答by user3227295

$('#tableData tbody tr:eq('+row+')').find('td:eq(8) input').is(':checked')

回答by Erick Petrucelli

You can do this way:

你可以这样做:

$('#tableData tbody tr:eq('+row+') td:eq(8) input').val()

回答by Alex

You're targeting the tdelement, instead the input, no? Try:

你的目标是td元素,而不是input,不是吗?尝试:

$('#tableData tbody tr:eq('+row+') td:eq(8) input').val()

回答by ray hatfield

You could query for the checkbox itself. (Even better, give the checkbox a name or an ID and query for that.)

您可以查询复选框本身。(更好的是,为复选框指定名称或 ID 并进行查询。)

alert($('input[type=checkbox]').val());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table><tr><td><input type="checkbox" value="99"></td></tr></table>

回答by taxicala

You are not specifying the checkbox at all in your query.

您根本没有在查询中指定复选框。

Try changing:

尝试改变:

$('#tableData tbody tr:eq('+row+')').find('td:eq(8)').eq(0).value

To:

到:

$('#tableData tbody tr:eq('+row+')').find('td:eq(8)').eq(0).find('checkbox').val();

回答by BoltX

Checkbox has 2 states. Checked and unchecked.

复选框有 2 种状态。选中和未选中。

To get the value use the .prop('value') - please see my JSFiddle at the bottom. (I also updated the example(s))

要获取值,请使用 .prop('value') - 请在底部查看我的 JSFiddle。(我还更新了示例)

I hope you are properly setting checked value on the control (from your code it is not clear if you're trying to set the value to something and expecting to see a checked checkbox or you're properly setting "checked" property.

我希望您在控件上正确设置了选中的值(从您的代码中,不清楚您是否正在尝试将值设置为某个值并希望看到选中的复选框,或者您是否正确设置了“已选中”属性。

<input type="checkbox" id="myCheckboxId" value="0"  checked>

In jquery use the selector to find the checkbox. Please search SO since there are so many examples. Looping with "row" as counter is not the best way, use .each on the table to look for rows with checked checkboxes.

在 jquery 中使用选择器来查找复选框。请搜索 SO,因为有很多例子。用“row”作为计数器循环不是最好的方法,在表上使用 .each 来查找带有选中复选框的行。

Even with what you have add and ID to your control (checkbox):

即使您在控件中添加了内容和 ID(复选框):

var isChecked = $('#tableData tbody tr:eq('+row+')').find('myCheckboxId').is(':checked');

Or:

或者:

var isChecked = $('#tableData tbody tr:eq('+row+') input:checkbox').is(':checked');
var checkBoxValue = $('#tableData tbody tr:eq('+row+') input:checkbox').prop('value'); 

http://api.jquery.com/checkbox-selector/try there first.

http://api.jquery.com/checkbox-selector/ 先试试。

JSFiddle Demo

JSFiddle 演示