单选框就是改变checked属性的值 当checked值为false 点击后值改为true 反之一样 当单选框都选中的时候就是通过判断checked为true的个数跟checkbox的长度是否一致 当checked为true的个数跟checkbox的长度一致的时候就会将全选框checked值改为true 否则为false 即
var count = $("input[name='id']:checkbox:checked").length;
if (count == $("input[name='id']:checkbox").length) {
$("input[name=allselect]:checkbox").prop("checked", true);
}
完整示例
str += '<td style="text-align:right; padding-right:0px;"><input type="checkbox" name="id" value='+ datalist[i].id+ ' onclick="userCheck(this)" ></td><td style="width:5%;texalign:left;">'+ datalist[i].id+ '</td>
str += '<div style="width:180px; height:25px;margin-top:10px;"><div style="width:49px; float: left;clear:right;"><input style="float: right; margin-top:10px;" type="checkbox" id="allselect" name="allselect" onclick="selectedAll()" ></div><div id="delselect" name="delselect" style="float:left; margin-left:10px; height:35px;line-height:35px; width:90px; border-radius:5px; border:1px; border-style:solid; border-color: #444444; color:#cccccc" onclick="delSelect()" > <img src="img/yhgl_delete_normal.png" style="vertical-align:middle; margin-right:5px; margin-left:20px; ">删除</div></div>';
//单选
function userCheck(ths) {
if (ths.checked == false) {
$("input[name=allselect]:checkbox").prop('checked', false);
} else {
var count = $("input[name='id']:checkbox:checked").length;
if (count == $("input[name='id']:checkbox").length) {
$("input[name=allselect]:checkbox").prop("checked", true);
}
}
}
//多选
function selectedAll() {
var select = $("input[name='allselect']").is(':checked');
if (select) {
$("input[type='checkbox']").prop("checked", true);
} else {
$("input[type='checkbox']").prop("checked", false);
}
}