更: 下面连接文章就是解决checkbox只能取到最后一个值的解决办法
/qq_16513911/article/details/81981733
<div class="layui-form-item"><label class="layui-form-label">站点</label><div class="layui-input-block"><input type="checkbox" lay-skin="primary" id="c_all" lay-filter="c_all" title="全部"><input type="checkbox" lay-skin="primary" id="f_all" lay-filter="f_all" title="反选"><br/><br/><input type="checkbox" name="cityId" lay-skin="primary" lay-filter="c_one" class="cityId" title="北京" value="10001"><input type="checkbox" name="cityId" lay-skin="primary" lay-filter="c_one" class="cityId" title="测试" value="10002"><input type="checkbox" name="cityId" lay-skin="primary" lay-filter="c_one" class="cityId" title="西安" value="10003"><input type="checkbox" name="cityId" lay-skin="primary" lay-filter="c_one" class="cityId" title="常州" value="10004"><input type="checkbox" name="cityId" lay-skin="primary" lay-filter="c_one" class="cityId" title="肇庆" value="10005"></div></div>
layui.use('form', function () {var form = layui.form;//全选form.on('checkbox(c_all)', function (data) {var a = data.elem.checked;if (a == true) {$(".cityId").prop("checked", true);form.render('checkbox');} else {$(".cityId").prop("checked", false);form.render('checkbox');}});//反选form.on('checkbox(f_all)', function (data) {var item = $(".cityId");item.each(function () {if ($(this).prop("checked")) {$(this).prop("checked", false);} else {$(this).prop("checked", true);}})form.render('checkbox');});//有一个未选中全选取消选中form.on('checkbox(c_one)', function (data) {var item = $(".cityId");for (var i = 0; i < item.length; i++) {if (item[i].checked == false) {$("#c_all").prop("checked", false);form.render('checkbox');break;}}//如果都勾选了 勾上全选var all=item.length;for (var i = 0; i < item.length; i++) {if (item[i].checked == true) {all--;}}if(all==0){$("#c_all").prop("checked", true);form.render('checkbox');}});});
更新一下,原来的单选太不友好了,弄成了开关的全选全不选,把最上面的4.5行改这个就行了.
改了改也贴上来吧
<input type="checkbox" name="close" lay-skin="switch" id="c_all" lay-filter="switchTest" lay-text="全不选|全选"><input type="checkbox" name="close" lay-skin="switch" id="c_boolean" lay-filter="c_boolean" lay-text="反选|反选">
layui.use('form', function () {var form = layui.form;//全选 全不选form.on('switch(c_boolean)', function (data) {var item = $(".cityId");item.each(function () {if ($(this).prop("checked")) {$(this).prop("checked", false);} else {$(this).prop("checked", true);}})form.render('checkbox');})//有一个未选中 取消全选form.on('checkbox(c_one)', function (data) {var item = $(".cityId");for (var i = 0; i < item.length; i++) {if (item[i].checked == false) {$("#c_all").prop("checked", false);form.render('checkbox');break;}}//如果都勾选了 勾上全选var all=item.length;for (var i = 0; i < item.length; i++) {if (item[i].checked == true) {all--;}}if(all==0){$("#c_all").prop("checked", true);form.render('checkbox');}});//反选form.on('switch(switchTest)', function(data){if(this.checked){var a = data.elem.checked;if (a == true) {$(".cityId").prop("checked", true);form.render('checkbox');} else {$(".cityId").prop("checked", false);form.render('checkbox');}}else{var item = $(".cityId");item.each(function () {if ($(this).prop("checked")) {$(this).prop("checked", false);} else {$(this).prop("checked", true);}})form.render('checkbox');}});});
如果觉得《layui checkbox 反选/全选/取消单个取消全选/全部选中勾上全选》对你有帮助,请点赞、收藏,并留下你的观点哦!