jQuery、Chrome 和复选框。奇怪的行为
我浏览了该网站,似乎找不到这个问题的答案。如果确实存在,请随时引导我回答上述问题。
我目前正在尝试执行一些基于树的复选框操作,并且遇到了一些我觉得奇怪的行为。
在 Google Chrome 中,我发现检查复选框会留下一些“残留物”,如果您愿意的话,这将不允许我使用“attrRemove('checked');”检查或取消选中该复选框。
有趣的是,Internet Explorer (9) 具有我想要的结果。请在 Chrome 和 IE 中查看这个 jsFiddle 以更好地比较效果。
http://jsfiddle.net/xixionia/9K5ft/
任何人都可以解释这些差异,以及我如何解释或许能够选中/取消选中 chrome 中已“手动”选中的复选框?
HTML
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
JavaScript
$(':checkbox').click(function(e) {
if($(this).is(':checked')) {
$(':checkbox').attr('checked', 'checked');
} else {
$(':checkbox').removeAttr('checked');
}
});
预先感谢您的帮助。 :)
编辑:
我能够找到这个解决办法。如果其他人有任何其他解决方案,请告诉我,我会将其标记为解决方案。 :)
$(':checkbox').click(function(e) {
var value = this.checked;
$(':checkbox').each(function(){ this.checked = value; });
});
编辑:
事实证明,这个问题是 jQuery 1.6 特有的。以前版本的 jQuery 不会遇到此问题。但是,我在这里发布了一个解决方案: 使用 jQuery 将复选框设置为“选中”?
I've looked around the site and can't seem to find an answer for this question. Feel free to direct me to said question, if it does indeed exist.
I'm currently trying to do some tree-based checkbox operations, and I've come accross some behavior that I find strange.
In Google Chrome, I find that checking a checkbox leaves some "residue", if you will, that will not allow me to check or uncheck that checkbox using "attrRemove('checked');"
Interestingly enough, Internet Explorer (9) has the result I am going for. Please see this jsFiddle in both Chrome and IE for a good comparison of the effects.
http://jsfiddle.net/xixionia/9K5ft/
Can anyone explain these difference, and how I might be able to check / uncheck a checkbox in chrome which has been checked "by hand" ?
HTML
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
JavaScript
$(':checkbox').click(function(e) {
if($(this).is(':checked')) {
$(':checkbox').attr('checked', 'checked');
} else {
$(':checkbox').removeAttr('checked');
}
});
Thanks in advance for your help. :)
edit:
I was able to find this work around. If anyone else has any other solutions, please let me know, and I'll mark it as the solution. :)
$(':checkbox').click(function(e) {
var value = this.checked;
$(':checkbox').each(function(){ this.checked = value; });
});
edit:
Turns out, this problem is specific to jQuery 1.6. Previous versions of jQuery do not suffer from this issue. However, I have posted a solution here: Setting "checked" for a checkbox with jQuery?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当使用复选框(尤其是基于复选框的复杂逻辑)时,我总是尝试避免在复选框上使用 jQuery 事件处理程序。 jQuery 中存在一个错误,该错误与单击复选框和使用的浏览器时事件触发的顺序有关:
您可以找到部分问题的描述此处。引用:
jQuery 将此称为一项功能,因为它从一开始就存在,将其更改为正确的行为只会杀死许多现有的应用程序。我经常遇到复选框和“选中”的问题,只是因为事件触发得太晚了。所以我只使用原生JS方式: input.checked 。
When working with checkboxes (especially complicated logic bases on checkboxes) I always try to avoid jQuery event handlers on the checkboxes. There is a bug in jQuery that has to do with the order of event triggering when clicking on checkboxes and the browser used:
You can find a description of part of the problem here. A quote:
jQuery calls this a feature because it has been around since the beginning and changing it to the correct behaviour would just kill so many existing applications. I often had problems with checkboxes and "checked" just because the event was fired too late. So I just use the native JS way: input.checked .
来自: http://api.jquery.com/prop/ 您应该使用 prop 而不是 attr
From : http://api.jquery.com/prop/ you should be using prop instead of attr
而不是这个:
你应该这样做:
干杯
Instead of this:
You should do this:
Cheers
我遇到了同样的问题,并在 chrome 中使用以下代码解决了。希望它会有所帮助。
I was facing the same problem and solved using the following code in chrome. Hope it will help.