jQuery、Chrome 和复选框。奇怪的行为

发布于 2024-11-05 03:39:38 字数 1442 浏览 1 评论 0原文

我浏览了该网站,似乎找不到这个问题的答案。如果确实存在,请随时引导我回答上述问题。

我目前正在尝试执行一些基于树的复选框操作,并且遇到了一些我觉得奇怪的行为。

在 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 技术交流群。

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

发布评论

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

评论(4

慢慢从新开始 2024-11-12 03:39:38

当使用复选框(尤其是基于复选框的复杂逻辑)时,我总是尝试避免在复选框上使用 jQuery 事件处理程序。 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:

It seems that when you trigger the click event on a checkbox, the default behavior (toggling the checked attribute) takes place after event handlers fire. This is opposite of what happens when the browser naturally handles the checkbox click event - the checked attribute is toggled and then the event handlers are executed.
This difference in the order of operations between inherent clicking and jQuery's event triggering can cause a big problem if the event handler depends on the status of checkbox in anyway (which, if you have a click handler on a checkbox to begin with means it probably does).

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 .

衣神在巴黎 2024-11-12 03:39:38

来自: http://api.jquery.com/prop/ 您应该使用 prop 而不是 attr

尽管如此,关于checked属性要记住的最重要的概念是它与checked属性并不对应。该属性实际上对应于 defaultChecked 属性,并且应该仅用于设置复选框的初始值。 checked 属性值不会随着复选框的状态而改变,而checked 属性却会改变。

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.

甲如呢乙后呢 2024-11-12 03:39:38

而不是这个:

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

你应该这样做:

$(':checkbox').attr('checked', false);

干杯

Instead of this:

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

You should do this:

$(':checkbox').attr('checked', false);

Cheers

書生途 2024-11-12 03:39:38

我遇到了同样的问题,并在 chrome 中使用以下代码解决了。希望它会有所帮助。

$("#allchkbox").click(function() {
    if(this.checked) {
        $(".ItemChkbox").each(function() {
            this.checked = true;
        });
    }
    else {
        $(".ItemChkbox").each(function() {
            this.checked = false;
        });
    }
});

I was facing the same problem and solved using the following code in chrome. Hope it will help.

$("#allchkbox").click(function() {
    if(this.checked) {
        $(".ItemChkbox").each(function() {
            this.checked = true;
        });
    }
    else {
        $(".ItemChkbox").each(function() {
            this.checked = false;
        });
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文