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

前端之家收集整理的这篇文章主要介绍了jquery一键控制checkbox全选、反选或全不选前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

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

Box').children(':checkBox').prop('checked',$(this).is(':checked')?true:false); });
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也取消选中 }

});

<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">全选/反选/全不选

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

原文链接:https://www.f2er.com/jquery/35798.html

猜你在找的jQuery相关文章