How can I get a checkbox's value in jQuery?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- How to fix IE ClearType + jQuery opacity problem i
- jQuery add and remove delay
- Is there a way to play audio on a mobile browser w
$('.class[value=3]').prop('checked', true);
Example
Demo
Use the following code:
Here is how to get the value of all checked checkboxes as an array:
The only correct ways of retrieving a checkbox's value is as following
as explained in the official documentations in jQuery's website. The rest of the methods has nothing to do with the property of the checkbox, they are checking the attribute which means they are testing the initial state of the checkbox when it was loaded. So in short:
elem.checked
) or you can use$(elem).prop("checked")
if you want to rely on jQuery.$(elem).is(":checked")
.Answers are misleading, Please check below yourself:
http://api.jquery.com/prop/
Despite the fact that this question is asking for a jQuery solution, here is a pure JavaScript answer since nobody has mentioned it.
Without jQuery:
Simply select the element and access the
checked
property (which returns a boolean).Here is a quick example listening to the
change
event:To select checked elements, use the
:checked
pseudo class (input[type="checkbox"]:checked
).Here is an example that iterates over checked
input
elements and returns a mapped array of the checked element's names.Example Here