iCheck使用
注意:使用iCheck时需要自己引用相关的皮肤,官方提供了几套皮肤,每一套还有不同的颜色,使用之前一定要引入相关css,不然会出现checkbox无法显示的窘境。
初始化iCheck
具体的请参考官方文档。
引入样式CSS和JavaScript
1 2 3
   | <script src="your-path/jquery.js"></script> <link href="your-path/minimal/red.css" rel="stylesheet"> <script src="your-path/icheck.js"></script>
   | 
 
 
添加HTML
1 2 3 4
   | <input type="checkbox"> <input type="checkbox" checked> <input type="radio" name="iCheck"> <input type="radio" name="iCheck" checked>
   | 
 
 
初始化iCheck
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
   |  <script>     $(document).ready(function(){       $('input').iCheck({         checkboxClass: 'icheckbox_minimal',          radioClass: 'iradio_minimal',         increaseArea: '20%'        });     }); </script>
 
  <script>     $(document).ready(function(){       $('input').iCheck({         checkboxClass: 'icheckbox_minimal-red',         radioClass: 'iradio_minimal-red',         increaseArea: '20%'        });     }); </script>
 
  <input type="checkbox" checked class="minimal">
 
  | 
 
 
全选功能实现
实现一个全选功能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
   | <div class="row">     <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">         <label>             <input type="checkbox" class="js-all">checkbox2         </label>     </div> </div> 
  <div class="row" data-role="checkbox">     <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">         <label>             <input type="checkbox">checkbox1         </label>     </div>     <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">         <label>             <input type="checkbox">checkbox2         </label>     </div> </div>
   | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
   | $(function () {     var checkAll = $('input.js-all');     var checkboxes = $('input[type="checkbox"]', '[data-role="checkbox"]');
      $('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', true);         } else {             checkAll.prop('checked', false);         }         checkAll.iCheck('update');     }); });
  | 
 
动态添加checkbox事件代理
动态添加checkbox事件不生效问题,应该使用事件代理。
1 2 3 4 5 6 7 8 9 10 11 12 13
   | <div class="row" data-role="checkbox">     <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">         <label>             <input type="checkbox">checkbox1         </label>     </div>     <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">         <label>             <input type="checkbox">checkbox2         </label>     </div>      </div>
   | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14
   | 
 
  var $checkboxContainer = $('.row[data-role="checkbox"]'); $checkboxContainer.on('ifChanged', function(event){          var checkboxes = $('input[type="checkbox"]', $checkboxContainer);     if(checkboxes.filter(':checked').length == checkboxes.length) {         checkAll.prop('checked', true);     } else {         checkAll.prop('checked', false);     }     checkAll.iCheck('update'); });
 
  | 
 
获取checked状态的checkbox
iCheckbox是没有提供获取checked状态的方法,所以你要使用jQuery提供的prop方法。
1 2 3 4 5 6 7 8
   | var checkboxes = $('input[type="checkbox"]', $checkboxContainer);
 
  var allChecked = checkboxes.filter(':checked');
  $.each(checkboxes, function (checkbox, i) {     console.log($(this).prop('checked'));  });
  | 
 
参考
http://icheck.fronteed.com/
http://github.com/fronteed/iCheck/
https://stackoverflow.com/questions/17820080/function-select-all-and-icheck
https://github.com/fronteed/iCheck/issues/44
https://www.cnblogs.com/xcsn/p/6307610.html