最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

jquery - How to get a value of a CheckBox from td (table), JavaScript? - Stack Overflow

matteradmin5PV0评论

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

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

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

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

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

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

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

Share Improve this question edited May 27, 2015 at 17:03 Samurai 3,7295 gold badges28 silver badges40 bronze badges asked May 27, 2015 at 17:01 Slavik OstapenkoSlavik Ostapenko 1131 gold badge3 silver badges10 bronze badges 1
  • possible duplicate of how to get the value of a checked checkbox – Seth McClaine Commented May 27, 2015 at 17:04
Add a ment  | 

6 Answers 6

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

Checkbox has 2 states. Checked and unchecked.

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

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.

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

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./checkbox-selector/ try there first.

JSFiddle Demo

You can do this way:

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

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

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

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

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

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();
Post a comment

comment list (0)

  1. No comments so far