jquery一键控制checkbox全选、反选或全不选

jquery attr()方法获取标签的 checked 会有问题,所以用了 prop() 方法

Hml的checkBox没有加name,只用了 div 嵌套。

如有更好的方法,望指点!!

rush:js;"> //全选 $('#allChecked').change(function(){ $('#Box').children(':checkBox').prop('checked',$(this).is(':checked')?true:false); });
rush:js;"> //反选 $('#invertChecked').change(function(){ if($(this).is(':checked')){ $('#Box').children(':checkBox').each(function(){ $(this).prop('checked',$(this).is(':checked')?false:true); }); } });
全选,.not(':checked').length 也可以。 $('#Box').children(':checkBox').prop('checked',true); }else{ // 如果有选中个数,-> 反选 $('#Box').children(':checkBox').each(function(){ $(this).prop('checked',$(this).is(':checked')?false:true); }); }else{ $('#Box').children(':checkBox').prop('checked',false); // 如控制键取消选中,剩余的checkBox也取消选中 }

});

rush:xhtml;">
<div id="Box">
<input type="checkBox" value="1">西瓜
<input type="checkBox" value="2">芒果
<input type="checkBox" value="3">橙
<input type="checkBox" value="4">山竹
<input type="checkBox" value="5">草莓
<input type="checkBox" value="6">火龙果


<input type="checkBox" id="allChecked">全选
<input type="checkBox" id="invertChecked">反选
<input type="checkBox" id="orChecked">全选/反选/全不选

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...