.prop('checked',false) or .removeAttr('checked')?
With the introduction of the prop method, now I need to kno开发者_JS百科w the accepted way of unchecking a checkbox. Is it:
$('input').filter(':checkbox').removeAttr('checked');
or
$('input').filter(':checkbox').prop('checked',false);
jQuery 3
As of jQuery 3, removeAttr
does not set the corresponding property to false
anymore:
Prior to jQuery 3.0, using
.removeAttr()
on a boolean attribute such aschecked
,selected
, orreadonly
would also set the corresponding named property tofalse
. This behavior was required for ancient versions of Internet Explorer but is not correct for modern browsers because the attribute represents the initial value and the property represents the current (dynamic) value.It is almost always a mistake to use
.removeAttr( "checked" )
on a DOM element. The only time it might be useful is if the DOM is later going to be serialized back to an HTML string. In all other cases,.prop( "checked", false )
should be used instead.
Changelog
Hence only .prop('checked',false)
is correct way when using this version.
Original answer (from 2011):
For attributes which have underlying boolean properties (of which checked
is one), removeAttr
automatically sets the underlying property to false
. (Note that this is among the backwards-compatibility "fixes" added in jQuery 1.6.1).
So, either will work... but the second example you gave (using prop
) is the more correct of the two. If your goal is to uncheck the checkbox, you really do want to affect the property, not the attribute, and there's no need to go through removeAttr
to do that.
use checked
: true, false property of the checkbox.
jQuery:
if($('input[type=checkbox]').is(':checked')) {
$(this).prop('checked',true);
} else {
$(this).prop('checked',false);
}
I recommend to use both, prop and attr because I had problems with Chrome and I solved it using both functions.
if ($(':checkbox').is(':checked')){
$(':checkbox').prop('checked', true).attr('checked', 'checked');
}
else {
$(':checkbox').prop('checked', false).removeAttr('checked');
}
Another alternative to do the same thing is to filter on type=checkbox attribute:
$('input[type="checkbox"]').removeAttr('checked');
or
$('input[type="checkbox"]').prop('checked' , false);
From the jQuery API Documentation:
The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the
.attr()
method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the.prop()
method provides a way to explicitly retrieve property values, while.attr()
retrieves attributes.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<input id="checkbox" type="checkbox" onchange='act(this)'>
<input id="checkbox" type="checkbox" onchange='act(this)'>
<script>
function act(element){
if($(element). prop('checked'))
{
console.log('You have Checked it');
}else
{
console.log('You Un-Checked it');
}
}
</script>
精彩评论