jQuery .prop('checked',false) 还是 .removeAttr('checked')?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6169826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:30:19  来源:igfitidea点击:

.prop('checked',false) or .removeAttr('checked')?

jquery

提问by Phillip Senn

With the introduction of the prop method, now I need to know the accepted way of unchecking a checkbox. Is it:

随着 prop 方法的引入,现在我需要知道取消选中复选框的可接受方式。是吗:

$('input').filter(':checkbox').removeAttr('checked');

or

或者

$('input').filter(':checkbox').prop('checked',false);

回答by John Flatness

jQuery 3

jQuery 3

As of jQuery 3, removeAttrdoes notset the corresponding property to falseanymore:

在jQuery 3,removeAttr没有设置相应的属性false了:

Prior to jQuery 3.0, using .removeAttr()on a boolean attribute such as checked, selected, or readonlywould also set the corresponding named property to false. 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.

在 jQuery 3.0 之前,.removeAttr()在布尔属性上使用,例如checked, selected, orreadonly也会将相应的命名属性设置为false。此行为是 Internet Explorer 的旧版本所必需的,但对于现代浏览器来说是不正确的,因为该属性表示初始值,而该属性表示当前(动态)值。

.removeAttr( "checked" )在 DOM 元素上使用几乎总是一个错误。唯一可能有用的情况是 DOM 稍后将被序列化回 HTML 字符串。在所有其他情况下,.prop( "checked", false )应改为使用。

Changelog

变更日志

Hence only .prop('checked',false)is correct way when using this version.

因此,只有.prop('checked',false)在使用此版本时才是正确的方法。



Original answer (from 2011):

原始答案(来自 2011 年):

For attributes which have underlying boolean properties (of which checkedis one), removeAttrautomatically sets the underlying property to false. (Note that this is among the backwards-compatibility "fixes" added in jQuery 1.6.1).

对于具有底层布尔属性(其中checked之一)removeAttr的属性,自动将底层属性设置为false. (请注意,这是在 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 removeAttrto do that.

所以,两者都可以工作……但是你给出的第二个例子(使用prop)是两者中更正确的一个。如果您的目标是取消选中复选框,那么您确实希望影响property,而不是该属性,并且没有必removeAttr要这样做。

回答by suraj rawat

use checked: true, false property of the checkbox.

使用checked:复选框的真、假属性。

jQuery:

jQuery:

if($('input[type=checkbox]').is(':checked')) {
    $(this).prop('checked',true);
} else {
    $(this).prop('checked',false);
}

回答by Ragnar

I recommend to use both, prop and attr because I had problems with Chrome and I solved it using both functions.

我建议同时使用 prop 和 attr,因为我在使用 Chrome 时遇到了问题,我使用这两个函数解决了它。

if ($(':checkbox').is(':checked')){
    $(':checkbox').prop('checked', true).attr('checked', 'checked');
}
else {
    $(':checkbox').prop('checked', false).removeAttr('checked');
}

回答by tonnoz

Another alternative to do the same thing is to filter on type=checkboxattribute:

另一种做同样事情的方法是过滤type=checkbox属性:

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

or

或者

$('input[type="checkbox"]').prop('checked' , false);

Remeber that 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.

请记住,属性和特性之间的差异在特定情况下可能很重要。在jQuery 1.6之前,.attr() 方法在检索某些属性时有时会考虑属性值,这可能会导致不一致的行为。从 jQuery 1.6 开始, .prop() 方法提供了一种显式检索属性值的方法,而 .attr() 方法检索属性。

Know more...

了解更多...