jQuery使用icheck插件在多个复选框中显示隐藏div

时间:2022-02-10 20:35:53

I design my checkbox with icheck plugin and worked with one and multiple checkbox(Check all) like this :

我用icheck插件设计我的复选框,并使用一个和多个复选框(全部检查),如下所示:

HTML :

<div>Using Check all function</div>
<div id="action" class="action-class" style="display:none">SHOW ME</div> 
<div id="selectCheckBox">
    <input type="checkbox" class="all" />Select All
    <input type="checkbox" class="check" />Check Box 1
    <input type="checkbox" class="check" />Check Box 2
    <input type="checkbox" class="check" />Check Box 3
    <input type="checkbox" class="check" />Check Box 4
</div>

JS:

$(function () {
    var checkAll = $('input.all');
    var checkboxes = $('input.check');

    $('input').iCheck();

    checkAll.on('ifChecked ifUnchecked', function(event) {        
        if (event.type == 'ifChecked') {
            checkboxes.iCheck('check');
        } else {
            checkboxes.iCheck('uncheck');
        }
    });

    checkboxes.on('ifChanged', function(event){
        if(checkboxes.filter(':checked').length == checkboxes.length) {
            checkAll.prop('checked', 'checked');
        } else {
            checkAll.removeProp('checked');
        }
        checkAll.iCheck('update');
    });
});

I have <div id="action" class="action-class" style="display:none">SHOW ME</div> and display:none.

我有

SHOW ME 和display:none。

Now, I need to show div if checked any checkbox(all and one) input.

现在,我需要显示div,如果选中任何复选框(全部和一个)输入。

how do can I show this!?

我怎么能显示这个!?

DEMO JSFIDDLE

2 个解决方案

#1


0  

Can do something like this that toggles the div based on whether or not any boxes are checked

可以做这样的事情,根据是否检查任何框切换div

function toggleDiv(){
    var showDiv=checkboxes.filter(':checked').length;
    $('#action').toggle(showDiv); /* if boolean argument, toggle will hide/show based on boolean */
}

checkboxes.on('ifChanged', function(event){
    if(checkboxes.filter(':checked').length == checkboxes.length) {
        checkAll.prop('checked', 'checked');
    } else {
        checkAll.removeProp('checked');
    }
    checkAll.iCheck('update');
    toggleDiv();

});

DEMO

#2


0  

    checkboxes.on('ifChanged', function(event){
        if(checkboxes.filter(':checked').length == checkboxes.length) {
            checkAll.prop('checked', 'checked');
        } else {
             checkAll.removeProp('checked');
        }
        checkAll.iCheck('update');

        //Add this -----------------------------------
        if(checkboxes.filter(':checked').length > 0) {
           $("#action").show();
        }
        else{
           $("#action").hide();
        }
   });

Demo : jsfiddle

演示:jsfiddle

#1


0  

Can do something like this that toggles the div based on whether or not any boxes are checked

可以做这样的事情,根据是否检查任何框切换div

function toggleDiv(){
    var showDiv=checkboxes.filter(':checked').length;
    $('#action').toggle(showDiv); /* if boolean argument, toggle will hide/show based on boolean */
}

checkboxes.on('ifChanged', function(event){
    if(checkboxes.filter(':checked').length == checkboxes.length) {
        checkAll.prop('checked', 'checked');
    } else {
        checkAll.removeProp('checked');
    }
    checkAll.iCheck('update');
    toggleDiv();

});

DEMO

#2


0  

    checkboxes.on('ifChanged', function(event){
        if(checkboxes.filter(':checked').length == checkboxes.length) {
            checkAll.prop('checked', 'checked');
        } else {
             checkAll.removeProp('checked');
        }
        checkAll.iCheck('update');

        //Add this -----------------------------------
        if(checkboxes.filter(':checked').length > 0) {
           $("#action").show();
        }
        else{
           $("#action").hide();
        }
   });

Demo : jsfiddle

演示:jsfiddle