Thursday, 16 February 2017

html - Get checkbox value in jQuery




How can I get a checkbox's value in jQuery?


Answer



To get the value of the Value attribute you can do something like this:



$("input[type='checkbox']").val();


Or if you have set a class or id for it, you can:



$('#check_id').val();

$('.check_class').val();


However this will return the same value whether it is checked or not, this can be confusing as it is different to the submitted form behaviour.



To check whether it is checked or not, do:



if ($('#check_id').is(":checked"))
{
// it is checked

}

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...