jquery checkbox 全选 检查是否选中代码
全选/全不选
1 //全选,全不选 elm:checkbox的属性名 val:对应属性的属性值 str:true/false 2 function chkALL(elm, val, str) { 3 $("input[" + elm + "='"+val+"']").each(function () { 4 $(this).attr("checked", str);// 有时会出现选中或全不选失效问题,可使用下面的方法进行设置。
$(this).prop("checked", str); 5 }); 6 }
用法:
1 <input type="checkbox" onclick="chkALL('grp','sel',this.checked)" />全选 2 <input type="checkbox" grp="sel" /> 3 <input type="checkbox" grp="sel" />
检查是否选中
1 //判断是否有选择的项,有返回true,无返回false 2 function CheckSelect(elm,val) 3 { 4 var flag = false; 5 $("input[" + elm + "='" + val + "']").each(function () { 6 if ($(this).attr("checked")) { 7 flag = true; 8 } 9 }); 10 return flag; 11 }
用法:
1 <input type="button" onclick="CheckSelect('grp','sel')" /> 2 <input type="checkbox" grp="sel" /> 3 <input type="checkbox" grp="sel" />
<注>代码使用时需要事先引入Jquery库