jQuery,Chrome和复选框。奇怪的行为 [英] jQuery, Chrome, and Checkboxes. Strange Behavior
问题描述
我浏览过网站,似乎找不到这个问题的答案。如果确实存在,请随意指示我。
我目前正在尝试做一些基于树的复选框操作,我来了
在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。 check;
$(':checkbox')每个(function(){this.checked = value;});
});
编辑:
原来,这个问题是特定于jQuery 1.6。以前的jQuery版本不会遇到这个问题。但是,我在这里发布了一个解决方案:
解决方案发件人: http://api.jquery.com/prop/ 您应该使用prop而不是attr
但是,关于checked属性的最重要的概念是它不对应checked属性。该属性实际上对应于defaultChecked属性,并且应该仅用于设置复选框的初始值。 checked属性值不随复选框的状态而改变,而checked属性则会改变。
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: How do I check a checkbox with JQuery or Javascript?
解决方案From : http://api.jquery.com/prop/ you should be using prop instead of attr
Nevertheless, the most important concept to remember about the checked attribute is that it does not correspond to the checked property. The attribute actually corresponds to the defaultChecked property and should be used only to set the initial value of the checkbox. The checked attribute value does not change with the state of the checkbox, while the checked property does.
这篇关于jQuery,Chrome和复选框。奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!