废话不多说,直接上图
对,还是上篇文章的图,这次我们不研究datatables,而是看这个复选框,比平常的复选框漂亮太多
看看我是如何实现的吧
插件叫iCheck
用法也简单
引入js和css
$("input[type=checkbox]").iCheck({ checkboxClass: "icheckbox_flat-green" });
可以,这是找到了所有的checkbox
那在看看如何全选
//全选 function check_all() { var $checkboxAll = $("#check-all"), $checkbox = $("tbody").find("[type='checkbox']"); $checkboxAll.on('ifClicked', function (event) { if (event.target.checked) { $checkbox.iCheck("uncheck"); } else { $checkbox.iCheck("check"); } }); }
我表头第一个check的id必须是check-all
触发这个checkbox的选中事件,对表格中的checkbox赋值check或者uncheck
好,项目中的就介绍到这,来的官网的东西
iCheck使用方法:
- $('input').iCheck('check'); //将输入框的状态设置为checked
- $('input').iCheck('uncheck'); //移除 checked 状态
- $('input').iCheck('toggle'); //toggle checked state
- $('input').iCheck('disable'); //将输入框的状态设置为 disabled
- $('input').iCheck('enable'); //移除 disabled 状态
- $('input').iCheck('update'); //apply input changes, which were done outside the plugin
- $('input').iCheck('destroy'); //移除iCheck样式
调用iCheck时,只需要将修改了默认值的参数列出来即可:
//基础使用方法 $('input').iCheck({ labelHover : false, cursor : true, checkboxClass : 'icheckbox_square-blue', radioClass : 'iradio_square-blue', increaseArea : '20%' });
下面是参数列表及其默认值:
{ handle: '', checkboxClass: 'icheckbox', radioClass: 'iradio', checkedClass: 'checked', checkedCheckboxClass: '', checkedRadioClass: '', uncheckedClass: '', uncheckedCheckboxClass: '', uncheckedRadioClass: '', disabledClass: 'disabled', disabledCheckboxClass: '', disabledRadioClass: '', enabledClass: '', enabledCheckboxClass: '', enabledRadioClass: '', hoverClass: 'hover', focusClass: 'focus', activeClass: 'active', labelHover: true, labelHoverClass: 'hover', increaseArea: '', cursor: false, inheritClass: false, inheritID: false, insert: '' }
iCheck皮肤
Black — minimal.css //黑色
Red — red.css //红色
Green — green.css //绿色
Blue — blue.css //蓝色
Aero — aero.css //win7中的那种玻璃效果
Grey — grey.css //银灰色
Orange — orange.css //橙色
Yellow — yellow.css //黄色
Pink — pink.css //粉红色
Purple — purple.css //紫色
iCheck回调事件
iCheck支持所有选择器(selectors),并且只针对复选框checkbox和单选radio按钮起作用
iCheck提供了大量回调事件,都可以用来监听change事件
事件名称 | 使用时机 |
---|---|
ifClicked | 用户点击了自定义的输入框或与其相关联的label |
ifChanged | 输入框的 checked 或 disabled 状态改变了 |
ifChecked | 输入框的状态变为 checked |
ifUnchecked | checked 状态被移除 |
ifDisabled | 输入框状态变为 disabled |
ifEnabled | disabled 状态被移除 |
ifCreated | 输入框被应用了iCheck样式 |
ifDestroyed | iCheck样式被移除 |
使用on()方法绑定事件:
$('input').on('ifChecked', function(event){ //ifCreated 事件应该在插件初始化之前绑定 alert(event.type + ' callback'); });
bootstrap iCheck中的radio和checkbox的大小可以调整吗?
.icheckbox_square-blue, .iradio_square-blue { display: block; margin: 0; padding: 0; width: 22px; height: 22px; background: url(blue.png) no-repeat; border: none; cursor: pointer; }
全选
//全选获取数值 var checkAll = $('input.all'); var checkboxes = $('input.check'); 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'); });
获取选中值
$(".ajax-delete").click(function(){ var url = $(this).attr('data-url'); var str=""; var ids=""; $("input[name='id']:checkbox").each(function(){ if(true == $(this).is(':checked')){ str+=$(this).val()+","; } }); if(str.substr(str.length-1)== ','){ ids = str.substr(0,str.length-1); } console.log(ids); });