全选、反选、多选、解决行与checkbox的冲突
来源:互联网 发布:今晚美国公布什么数据 编辑:程序博客网 时间:2024/06/05 14:31
//全选 $("#checkAll").on("click", function () { if ($(this).prop("checked") === true) { $("input[name='checkList']").prop("checked", $(this).prop("checked")); } else { $("input[name='checkList']").prop("checked", false); } }); //复选框:1、(tr)点击选中,2、多选3、反选, $(document).ready(function () { $('#mylist tbody').on('click', 'tr', function () { /*当前(tr)复选框是选中的*/ if ($(this).children().first().children().prop("checked") === true) { /*当前是全选的*/ if ($("#checkAll").prop("checked") === true) { /*当前(tr)去除选中*/ $(this).children().first().children().attr("checked", false); /*全选按钮去除选中*/ $("#checkAll").prop("checked", false).parent().removeClass('checked'); } else if ($("#checkAll").prop("checked") !== true) { /*(tr)选中(当前)*/ $(this).children().first().children().attr("checked", false); /*当前不是全选的,当前(tr)选中*/ $("#checkAll").prop("checked", false).parent().removeClass('checked'); } } else if($(this).children().first().children().prop("checked") !== true){ /*(tr)选中(当前)*/ $(this).children().first().children().attr("checked", true); /*获取选中(tr)长度和checkbox选中长度参数*/ var $tr = $(this).parents('tr'); $tr.toggleClass('selected'); var $tmp = $('[name=checkList]:checkbox'); /*选中数量达标的反选(选中全选)*/ if ($tmp.length == $tmp.filter(':checked').length) { $("#checkAll").prop("checked", true).parent().addClass('checked'); } else { /*选中数量不达标的反选(去除全选)*/ $("#checkAll").prop("checked", false).parent().removeClass('checked'); } } }); });/**这里使用了datatable.js插件100%匹配,如果要使用的话,请自行修改jquray元素,如果有更好的方法请告诉我1448711794@qq.com**/
<table id="mylist"><th><input type="checkbox" id="checkAll"></th><tr><td><input type="checkbox" name="checkList" id="checkList" ></td></tr><tr><td><input type="checkbox" name="checkList" id="checkList" ></td></tr><tr><td><input type="checkbox" name="checkList" id="checkList" ></td></tr><tr><td><input type="checkbox" name="checkList" id="checkList" ></td></tr><tr><td><input type="checkbox" name="checkList" id="checkList" ></td></tr><tr><td><input type="checkbox" name="checkList" id="checkList" ></td></tr> </table>
上图:
上面的那个代码有个行与控件的冲突:
解决冲突后的完整代码:
//复选框:1、(tr)点击选中,2、多选3、反选, $('#mylist tbody').on('click', 'tr', function (event) { var isCheckbox = $(event.target).is(":checkbox");//判断是否是点击的checkbox控件 if ($("#checkAll").prop("checked") === true) { $(this).children().first().children().attr("checked", false); $("#checkAll").prop("checked", false).parent().removeClass('checked'); } else if ($("#checkAll").prop("checked") != true) { if ($(this).children().first().children().prop("checked") === true) { if(isCheckbox){//是 $(this).children().first().children().attr("checked", true); }else if(!isCheckbox){//不是 $(this).children().first().children().attr("checked", false); } } else if ($(this).children().first().children().prop("checked") != true) { if(isCheckbox){//是 $(this).children().first().children().attr("checked", false); }else if(!isCheckbox){//不是 $(this).children().first().children().attr("checked", true); } } var $tr = $(this).parents('tr'); $tr.toggleClass('selected'); var $tmp = $('[name=checkList]:checkbox'); if ($tmp.length == $tmp.filter(':checked').length) { $("#checkAll").prop("checked", false).parent().addClass('checked'); } else if ($tmp.length != $tmp.filter(':checked').length) { $("#checkAll").prop("checked", false).parent().removeClass('checked'); } } });
0 0
- 全选、反选、多选、解决行与checkbox的冲突
- checkbox 全选 与 反选
- checkbox全选与反选
- checkbox全选与反选
- checkbox全选与反选
- checkbox全选与反选
- Checkbox全选与反选
- jquery checkbox 全选反选,解决第二次失败的问题
- 使用js实现checkbox的全选与反选功能
- checkbox复选框的全选与反选
- jquery实现复选框(checkbox)的全选与反选
- Jquery之CheckBox全选与反选
- checkbox全选反选
- checkbox 全选 反选
- checkbox全选反选
- checkbox,全选,反选
- checkbox全选反选
- checkbox的全选和反选
- 关系数据库还是NoSQL数据库
- https
- poj2502(SPFA+建图)
- 九度OJ 题目1446:Head of a Gang
- hdu 2015 偶数求和
- 全选、反选、多选、解决行与checkbox的冲突
- Welcome to the Windows Bridge for iOS project preview
- 【HDU4722】【Good Numbers】
- 欢迎使用CSDN-markdown编辑器
- 浅谈"壳"(一)
- Servlet+JSP+JavaBean开发模式(MVC)介绍
- Dalvik虚拟机总结
- shared_ptr/unique_ptr一点体会
- Kinect2.0+EmguCV获取景深图