من قبل
Hazem Salama , Senior Member of Technical Staff , Verizon Communications
If you mean the value that you have in the value attribute of the checkbox then you can use this
//Assuming your check box id is chkBox
$('#chkBox').val();
If you mean the state, whether checked or not, you can use plain old javascript
document.getElementById('chkBox').checked; // will return false or true depending on the state
// OR in jquery
$('#chkBox').is(':checked');
There are two parts of the question I guess. They are:
1. You want to get checkbox value regardless of whether it is checked or not?
2. You want to get THE "Checked" checkbox value.
Answers:
1. You can get checkbox value:
$("#checkbox_id").val();
$(".checkbox_class").val(); // this is return in array if particular class is applied on many other fields or checkbox.
2. You can get Checed checkbox value:
if( $("#checkbox_id").attr("checked") )
{
$("#checkbox_id").val();
}
on check box change event
$('#checkboxid').change(function(){
if($('#checkboxid').is('checked'))
{
var val = $('#checkboxid').val();
console.log(val);
}
});
function checkboxValues() {
var values = [];
$('#checkbox :checked').each(function() {
values.push($(this).val());
});
console.log(values);
}
don't forget to enable firebug to see the results .
$("input[type='checkbox']").val();
Or if you have set a class or id for it, you can:
$('#check_id').val();
$('.check_class').val();
Also you can check whether it is checked or not like:
if ($('#check_id').is(":checked"))
{
// it is checked
}