简体   繁体   English

当在Firefox中单击另一个复选框时,如何一次取消选择多个复选框?

[英]How to deselect multiple checkboxes at once when another checkbox is clicked in Firefox?

I mention Firefox in the title because my current implementation works perfectly in Chrome and IE but not in Firefox. 我在标题中提到了Firefox,因为我当前的实现可以在Chrome和IE中完美运行,但不能在Firefox中运行。

Currently I have two groups of checkboxes in one dropdown menu that are identified by two different classes, upperFilter and lowerFilter. 目前,我在一个下拉菜单中有两组复选框,由两个不同的类(upperFilter和lowerFilter)标识。 What I want to happen is when a checkbox in the upperFilter group is clicked, any checkboxes that are already selected in the lowerFilter group are deselected and vice versa. 我想发生的是,当单击upperFilter组中的复选框时,取消选择LowerFilter组中已经选择的所有复选框,反之亦然。

This is how I am currently handling this in javascript: 这是我目前在javascript中处理此问题的方式:

$("INPUT:checkbox").click(function () {

        if ($(this).hasClass("upperFilter") && $('INPUT:checked').filter('.lowerFilter').length > 0) {
            $('INPUT:checked').filter('.lowerFilter').click();
        } else if ($(this).hasClass("lowerFilter") && $('INPUT:checked').filter('.upperFilter').length > 0) {
            $('INPUT:checked').filter('.upperFilter').click();
        }

        // other stuff...

    });

Like I stated above, this works in Chrome and IE but in Firefox what happens is that when more than one checkbox is selected and you select a checkbox in the other group only one of the previously checked boxes get deselected. 就像我在上面提到的那样,这在Chrome和IE中有效,但是在Firefox中,发生的情况是,当选中多个复选框并且您在另一组中选择了一个复选框时,只有先前选中的一个复选框被取消了。

What's strange is that I can select 3 boxes from upperFilter and then open up the Firebug console and type $('INPUT:checked').filter('.upperFilter').click(); 奇怪的是,我可以从upperFilter中选择3个框,然后打开Firebug控制台并键入$('INPUT:checked').filter('.upperFilter').click(); and it deselects all 3 of them at once. 并立即取消选择所有三个。 Also, all of the JS is loaded at the end of the page, just FYI. 另外,所有JS都在页面末尾加载,仅供参考。

I have no idea why it would work in the console and not in-page. 我不知道为什么它会在控制台而不是页面内工作。

Using .click is not the right way to go. 使用.click不是正确的方法。 Here's why: 原因如下:

You code says when some one clicks on the top, trigger the click event on the bottom (and vice versa) . 您的代码说, when some one clicks on the top, trigger the click event on the bottom (and vice versa) So what's happening right now is someone clicks the top, and then you trigger a click on the buttom, which in turn is going to trigger a click on the top, which will then... you get the point. 因此,现在发生的事情是有人单击顶部,然后触发了对按钮的单击,而按钮又触发了对顶部的单击,然后……您就明白了。

Its hard to tell what you are trying to do and we can't see what happens on the page as different checkboxes are clicked, but you need to take advantage of both the click and change event. 很难说出您要执行的操作,并且由于单击了不同的复选框,我们看不到页面上会发生什么,但是您需要同时利用clickchange事件。

  1. Keep the logic you currently have where click ing on the top toggles the bottom and vice versa. 保持您当前具有的逻辑,在顶部click可切换底部,反之亦然。

  2. Instead of triggering the click event with ( .click ), trigger the change event. 而不是使用( .click )触发click事件,而是触发change事件。 So replace this: 因此,替换为:

     $('INPUT:checked').filter('.lowerFilter').click(); 

    with this: 有了这个:

     $('INPUT:checked').filter('.lowerFilter').prop('checked', false).trigger('change'); 
  3. Then, elsewhere in your app, you need to listen for the "change" event on the checkboxes and update that part of your app accordingly. 然后,在应用程序的其他位置,您需要侦听复选框上的“ change”事件,并相应地更新应用程序的该部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM