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

标签 javascript jquery html css icheck

我使用 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');
    });
});

我有<div id="action" class="action-class" style="display:none">SHOW ME</div>display:none .

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

我怎样才能显示这个!?

演示 JSFIDDLE

最佳答案

可以做这样的事情,根据是否选中任何框来切换 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

关于javascript - jQuery 使用 icheck 插件在多个复选框中显示隐藏 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26146373/

相关文章:

javascript - 正则表达式获取具有不同协议(protocol)的域

javascript - 折叠不适用于 Bootstrap 的移动导航栏

html - CSS:为什么滚动条出现时菜单边框不出现?

javascript - Jquery 自动完成字体 : change font in the box

javascript - 下拉菜单选择决定将显示什么表单

javascript - 在 componentWillReceiveProps 函数中调用 setTimeout 时无法解析模块计时器

javascript - "On shown"手动使用 Bootstrap 模式时的事件

javascript - module.exports 是 Node.js 独有的

javascript - jQuery Ajax-load 替换 IE9 中的页面而不是 div(无需开发人员工具模式切换)

html - 是否需要使用带有 <div> 的结束标记来清除 float ?