jQuery 通过jQuery添加checked属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3711002/
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 15:57:30  来源:igfitidea点击:

Adding checked attribute via jQuery

jqueryattributesaddchecked

提问by Chris

I am using the following widget http://www.erichynds.com/examples/jquery-ui-multiselect-widget/demos/It has worked great so far, but I need some help in adding attributes. In using Firebug, I've noticed that by simply clicking on the checkbox the checked attribute does not appear as I would expect it to. In my code, I've modified the widget, I've been able to add code to remove the attribute of checked.

我正在使用以下小部件 http://www.erichynds.com/examples/jquery-ui-multiselect-widget/demos/到目前为止效果很好,但我需要一些帮助来添加属性。在使用 Firebug 时,我注意到只需单击复选框,checked 属性就不会像我期望的那样出现。在我的代码中,我修改了小部件,我已经能够添加代码来删除已检查的属性。

this.removeAttribute(\'checked\'); this.checked=false;

if the item was checked beforehand. I've been successful in using this code

如果事先检查了该项目。我已成功使用此代码

this.setAttribute(\'checked\', \'checked\'); this.checked=true; 

if the item was unchecked when the page loads.

如果页面加载时未选中该项目。

My problem is coming when I need to be able to utilize both sets of code on the checkbox, I've attempted the following

当我需要能够在复选框上使用两组代码时,我的问题就出现了,我尝试了以下操作

onclick="if($(this).attr(\'checked\') == \'true\') { this.setAttribute(\'checked\', \'checked\'); this.checked=true; } else { this.removeAttribute(\'checked\'); this.checked=false; }

the code will remove the attribute of checked (if checked before hand on page load), but when I attempt to click on the checkbox to add the attribute (if not checked on page load), nothing happens.

代码将删除已检查的属性(如果在页面加载之前检查过),但是当我尝试单击复选框以添加属性时(如果在页面加载时未选中),没有任何反应。

Thanks for any help, and sorry for my poor coding.

感谢您的帮助,并为我糟糕的编码感到抱歉。

回答by Deviprasad Das

In jQuery for removing an attribute use

在 jQuery 中删除属性使用

this.removeAttr('checked');

and for setting an attribute use

并用于设置属性使用

this.attr('checked', 'checked');

回答by Phillip Senn

$(this).prop('checked',false)

or

或者

$(this).prop('checked',true)

回答by bobince

I've noticed that by simply clicking on the checkbox the checked attribute does not appear

我注意到只需单击复选框,就不会出现已选中的属性

That's absolutely correct. The checkedattribute does notcorrespond to the current checkedness of the input; it actually corresponds to the initial checkedness from the HTML document, the checkedness that will be reinstated if the form is reset.

这是完全正确的。该checked属性与输入的当前检查性对应;它实际上对应于 HTML 文档的初始检查性,如果表单被重置,检查性将被恢复。

IE adds the attribute because IE is wrong. getAttribute/setAttributeare broken in IE—never use them on an HTML document!—they actually set the property, not the attribute.

IE添加属性是因为IE不对。getAttribute/setAttribute在 IE 中被破坏——永远不要在 HTML 文档中使用它们!——它们实际上设置的是属性,而不是属性。

This is the defaultCheckedproperty. But why are you trying to set the checkedattribute? There is almost never a good reason to. Normally the plain old checkedproperty is what you're looking for.

这是defaultChecked物业。但是为什么要尝试设置checked属性?几乎从来没有一个很好的理由。通常,checked您正在寻找的是普通的旧财产。

回答by casablanca

In using Firebug, I've noticed that by simply clicking on the checkbox the checked attribute does not appear as I would expect it to.

在使用 Firebug 时,我注意到只需单击复选框,checked 属性就不会像我期望的那样出现。

Why do you need the checkedattribute at all? The checkedproperty provides all the functionality that you would normally need, i.e. checking the box will set element.checkedto trueand vice versa, i.e. modifying element.checkedwill change the state of the box.

为什么你需要这个checked属性?该checked属性提供您通常需要的所有功能,即选中框将设置element.checkedtrue,反之亦然,即修改element.checked将更改框的状态。