通过 jQuery 添加选中属性

发布于 2024-09-18 23:28:37 字数 860 浏览 3 评论 0原文

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

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

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

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

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

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

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

代码将删除选中的属性(如果在页面加载之前检查过),但是当我尝试单击时在添加属性的复选框上(如果在页面加载时未选中),则不会发生任何情况。

感谢您的帮助,并对我糟糕的编码表示歉意。

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

☆獨立☆ 2024-09-25 23:28:37

在 jQuery 中用于删除属性使用

this.removeAttr('checked');

和设置属性使用

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

In jQuery for removing an attribute use

this.removeAttr('checked');

and for setting an attribute use

this.attr('checked', 'checked');
千笙结 2024-09-25 23:28:37
$(this).prop('checked',false)

或者

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

or

$(this).prop('checked',true)
≈。彩虹 2024-09-25 23:28:37

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

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

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

这是 defaultChecked 属性。但是为什么要尝试设置 checked 属性呢?几乎没有充分的理由这样做。通常,您要寻找的是普通的旧 checked 属性。

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

That's absolutely correct. The checked attribute does not correspond 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.

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

This is the defaultChecked property. But why are you trying to set the checked attribute? There is almost never a good reason to. Normally the plain old checked property is what you're looking for.

公布 2024-09-25 23:28:37

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

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

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.

Why do you need the checked attribute at all? The checked property provides all the functionality that you would normally need, i.e. checking the box will set element.checked to true and vice versa, i.e. modifying element.checked will change the state of the box.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文