checking if a checkbox is checked?
This question already has an answer here:
if($('#element').is(':checked')){
//checkbox is checked
}
or
if($('#element:checked').length > 0){
//checkbox is checked
}
or in jQuery 1.6+:
if($('#element:checked').prop('checked') === true){
//checkbox is checked
}
It depends on where you are trying to do this. Generally you can do:
$('#element').is(':checked');
or
$('#element')[0].checked;
or
$('#element').prop('checked');
or older version of jquery ( < 1.6) that doesn't support prop, attr used to do the job of prop as well to set/reset properties of element (Incase of standalone attributes like checked, selected, disabled etc...);
$('#element').attr('checked') //will return boolean value
If it is in the context of the checkbox like, if in a change
event you can just do:
this.checked
Using element's id or class is a good way of doing all this, but since we're using jQuery, you might use their API and here is the solution:
$('input[type="checkbox"]').is(':checked') {
// do the stuff here..
}
You can also use #element
instead of the input[type="checkbox"]'
.
This way, you can get to know that the checkbox is checked.
链接地址: http://www.djcxy.com/p/9522.html上一篇: 如何确定复选框是否已选中或未选中?
下一篇: 检查复选框是否被选中?