我想使用iCheck实现一个“全部选择”。
这就是我到目前为止所做的:
$(function () {
$('input').iCheck();
$('input.all').on('ifChecked ifUnchecked', function(event) {
if (event.type == 'ifChecked') {
$('input.check').iCheck('check');
} else {
$('input.check').iCheck('uncheck');
}
});
$('input.check').on('ifUnchecked', function(event) {
$('input.all').iCheck('uncheck');
});
});小提琴:jsfiddle.net/N7uYR/1/
当选中“ 1”、“复选框2”、“复选框3”、“复选框4”时,“selected”也被选中。
就像这样:jsfiddle.net/ivanionut/N7uYR/
我该怎么做?
发布于 2013-07-23 21:01:26
这是我想出来的。
$(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');
});
});jsFiddle
发布于 2015-09-01 12:51:39
对于那些仍在努力解决这个问题的人来说,我已经改变了https://stackoverflow.com/a/17821003/3061836的公认答案,从Dzulqarnain纳斯尔(Dzulqarnain)那里略为改变了一点。
我已经将removeProp方法更改为prop方法:
checkAll.removeProp('checked');被修改为
checkAll.prop('checked', false);removeProp方法对我无效,但是将选中的属性设置为false有效。
在编写本报告时,我使用的是iCheck 1.0.2和jQuery 2.1.4
发布于 2013-07-23 20:12:20
你可以用你的父复选框来做这件事。例如
<div class='allchecked'>
<input type="checkbox"/>
<input type="checkbox"/>
并使用jquery设置attr(“选中”)或不设置attr。
https://stackoverflow.com/questions/17820080
复制相似问题