2014-10-01 57 views
0

我设计我的复选框与I检查插件,并与一个和多个复选框(查看全部)这样的工作:jQuery的显示隐藏的div使用I检查插件

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如果检查了任何复选框(全部和一个)输入。

我该如何显示这个!?

DEMO JSFIDDLE

回答

0

可以做这样的事情是切换基于股利任何框是否被选中

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

+1

这不行!请先点击检查所有你看到的'div'(true)比点击复选框4 u隐藏'div'这不是真的,因为我们有3个复选框(1 - 2 - 3)。 – 2014-10-01 17:10:53

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(); 
     } 
    }); 

演示:jsfiddle