简体   繁体   English

带有所有按钮的Jquery Button插件可以切换复选框

[英]Jquery Button Plugin with All Button to toggle checkboxes

I am using jquery button and need to toggle associated checkeboxes but my code hangs the browser. 我正在使用jquery按钮,需要切换关联的checkeboxes但我的代码挂起浏览器。 I need 1) On click of all select all 2) on click of select all (all selected) uncheck all checkboxes 3) if all selected clicking on any sub checkboxes should unselect (ALL checkbox) and the one that was clicked as checkbox should remain. 我需要1)点击全部选择全部2)点击全选(全部选中)取消选中所有复选框3)如果所有选中的任何子复选框都应该取消选中(全部复选框),并且单击的复选框应该保留。

Here is the fiddle . 这是小提琴

The issue I am facing is, when i do this process, the checkboxes hangs up making the browser to hang even. 我面临的问题是,当我执行此过程时,复选框会挂起,使浏览器挂起。 for eg: In the fiddle click all, den click any checkbox in between. 例如:在小提琴中单击全部,然后单击中间的任何复选框。 so after this when again I click on check all, it hangs up 所以在此之后再次点击检查全部,它挂断了

Any help is really appreciated. 任何帮助都非常感谢。

    <script>
jQuery(document).ready(function(){
// If checkd all and we click on any sub options make ALL option UNCHECKED

jQuery( ".format" ).buttonset();
    //check ALl checkbox onClick
    $("body").on("click", ".chktaxoall,.chkcmfall", function (e, source) {
        var all = $(this).is(":checked");
        var set = $(this).data("set");

        if (source != "code") {
            $("input[type=checkbox][data-set='" + set + "']:not(this)").each(function () {
                if ($(this).is(":checked") != all) 
                    $(this).trigger("click", "code");
            });
        }
    });

    $("body").on("click", ".wrap_acf input:checkbox", function (e, source) {
        var set = $(this).data("set");
        var allChecked = $(".chkcmfall[data-set='" + set + "']").is(":checked");

        if (source != "code" && allChecked) {
            $(".wrap_acf input[type=checkbox][data-set='" + set + "']:not(this)").trigger("click", "code");
            $(".chkcmfall[data-set='" + set + "']").trigger("click", "code");
        }
        else if (source != "code")
        {
            if ($(".wrap_acf input[type=checkbox][data-set='" + set + "']:checked").length == $(".wrap_acf input[type=checkbox][data-set='" + set + "']").length)
                $(".chkcmfall[data-set='" + set + "']").trigger("click", "code");
        }
    })
    })

</script>

I think this is more or less what you wanted based on your responses to the others. 根据您对其他人的回答,我认为这或多或少是您想要的。 http://jsfiddle.net/drmyersii/27xkkxbw/2/ http://jsfiddle.net/drmyersii/27xkkxbw/2/

The major change is here: 主要变化在于:

$("body").on("click", ".css-checkbox:not(.chkcmfall)", function (e, source) {
    var set = $(this).data("set");
    var all = $(".chkcmfall[data-set='" + set + "']").is(":checked");
    var currentChecked = $(this).is(":checked");

    if(!currentChecked && all)
    {
        $('.css-checkbox[data-set="' + set + '"]').prop('checked', false);
        $(this).prop('checked', true);
        jQuery( ".format" ).buttonset();
    }
});

If this is what you wanted, I can clean up the fiddle for you too. 如果这是你想要的,我也可以为你清理小提琴。

I played around with your fiddle and figured out your problem: You trigger a click event and pass along "code" as a parameter; 我玩弄你的小提琴并找出你的问题:你触发一个点击事件并传递“代码”作为参数; you then check on the parameter "code" and click your elements if they aren't sending the parameter "code" -so you're trying to prevent repeated clicks. 然后检查参数“code”并点击你的元素,如果他们没有发送参数“code” - 那么你试图防止重复点击。 However, if you add some console.log statements you'll quickly find that the parameter "source" is never defined - it's consistently undefined - so your checkboxes are clicked over and over again. 但是,如果添加一些console.log语句,您将很快发现参数“source”从未定义 - 它始终未定义 - 因此您的复选框一遍又一遍地被点击。

If you switch to using 2 Events as suggested by Bardo you can really differentiate where your clicks are coming from and you won't experience the lag. 如果您按照Bardo的建议切换到使用2个事件,您可以真正区分点击来自哪里,您将不会遇到延迟。

I've forked your fiddle and made the some changes. 我已经分道扬琴,做了一些改变。 I think this does what you want it to do and it performs well: http://jsfiddle.net/bmartinelle/brzexyf9/ 我认为这可以做你想做的事情而且效果很好: http//jsfiddle.net/bmartinelle/brzexyf9/

 $("input[type=checkbox][data-set='" + set + "']").each(function () {
      $(this).trigger("cclick", "code");//trigger a custom click event! you can't pass a parameter to the default click event
 });


 $("body").on("cclick", ".wrap_acf input:checkbox", function (e, source) {
       var set = $(this).data("set");
       var allChecked = $(".chkcmfall[data-set='" + set + "']").is(":checked");

       if(allChecked &! $(this).is(":checked"))
       {
             $(this).click();
       }else if( (!allChecked) && $(this).is(":checked"))
       {
           $(this).click();
        }

  });

EDIT: also support unchecking of the "all" button if an element is unchecked: 编辑:如果未选中元素,也支持取消选中“全部”按钮:

$("body").on("click", ".css-checkbox:not(.chkcmfall)", function (e, source) {
         var set = $(this).data("set");
         var all = $(".chkcmfall[data-set='" + set + "']").is(":checked");
         var currentChecked = $(this).is(":checked");

           if(!currentChecked && all)
           {
               //we need to uncheck all:

                $(".chkcmfall[data-set='" + set + "']").prop("checked", false);
                $( ".format" ).buttonset();
           }
 });

There's nothing particularly wrong with your logic. 你的逻辑没有什么特别的错误。 Your slowness is coming from the method you are using setting the checkboxes. 您的缓慢来自您使用设置复选框的方法。 You call click() on each of them, which of course, fires off the event again every single time and recalculates everything multiple times. 你可以在每个上面调用click(),当然,每次都会再次触发事件,并多次重新计算所有内容。 Instead I recommend this: 相反,我推荐这个:

$("body").on("click",".chkcmfall", function() {
    var set = $(this).data("set");
    var allButtonIsChecked = $(".chkcmfall[data-set='" + set + "']").prop('checked');
    var checkBoxSet = $(".wrap_acf input[type=checkbox][data-set='" + set + "']");
    checkBoxSet.prop('checked',allButtonIsChecked);
    checkBoxSet.each(function () {
        var myLabel = $("label[for='" + $(this).prop('id') + "']");
        myLabel.removeClass('ui-state-active');
        myLabel.removeAttr('aria-pressed');
        if ($(this).prop('checked')){
            myLabel.addClass('ui-state-active');
            myLabel.attr('aria-pressed',"true");
        }
    });
});

$("body").on("click",".wrap_acf input[type=checkbox][data-set]", function () {
    var set = $(this).data("set");
    var allButton = $(".chkcmfall[data-set='" + set + "']");
    if (allButton.prop('checked')){
        allButton.trigger("click");
        $(this).prop('checked',"true");
        var myLabel = $("label[for='" + $(this).prop('id') + "']");
        myLabel.addClass('ui-state-active');
        myLabel.attr('aria-pressed',"true");            
    }

});

Ok, let's see if I'm understanding what you need. 好的,让我们看看我是否理解你的需求。

You want a button that, on click checks/unchecks all checkboxes on your page, and you want that, when clicking on a checkbox, if it's checked, it remains checked while all others get's unchecked. 您需要一个按钮,在点击检查/取消选中页面上的所有复选框时,您希望在单击复选框时,如果选中此复选框,则会保持选中状态,而其他所有复选框都未选中。 It's right? 这是正确的?

Ok, in this case you need two events. 好的,在这种情况下你需要两个事件。 One for button clicking and another one for checkbox elements clicking. 一个用于按钮单击,另一个用于复选框元素单击。

The first one should be as simple as: 第一个应该是这样简单:

$("#myButton").on("click", function() {
    //If just one or none checkboxes are enabled then button click should enable all
    //otherwise button click should disable all
    var enableAll = $('input[type="checkbox"]:checked').length <= 1;
    $('input[type="checkbox"]').attr("checked", enableAll);
});

Then, for the click on a single checkbox you can just do this: 1) Uncheck all checkboxes 2) Check this checkbox 然后,单击一个复选框,您可以执行以下操作:1)取消选中所有复选框2)选中复选框

$('input[type="checkbox"]').on("click", function() {
    $('input[type="checkbox"]').attr("checked", false);
    $(this).attr("checked", true);
});

Probably you'll need to workaround a bit the code, as I'm writting it on the run. 可能你需要解决一些代码,因为我正在运行它。 The basic idea is that, if I'm understanding correctly your needings, probably your code should be a lot simpler than what you have now... try with this ideas. 基本的想法是,如果我正确理解你的需要,可能你的代码应该比你现在的代码简单得多......试试这个想法。

I hope it helps. 我希望它有所帮助。

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

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