您的位置:首页 > Web前端 > JQuery

jquery复选框操作

2016-05-19 15:13 483 查看
$('input[type="checkbox"]').change(function(e) {

var checked = $(this).prop("checked"),
container = $(this).parent(),
siblings = container.siblings();

container.find('input[type="checkbox"]').prop({
indeterminate: false,
checked: checked
});

function checkSiblings(el) {

var parent = el.parent().parent(),
all = true;

el.siblings().each(function() {
return all = ($(this).children('input[type="checkbox"]').prop("checked") === checked);
});

if (all && checked) {

parent.children('input[type="checkbox"]').prop({
indeterminate: false,
checked: checked
});

checkSiblings(parent);

} else if (all && !checked) {

parent.children('input[type="checkbox"]').prop("checked", checked);
parent.children('input[type="checkbox"]').prop("indeterminate", (parent.find('input[type="checkbox"]:checked').length > 0));
checkSiblings(parent);

} else {

el.parents("li").children('input[type="checkbox"]').prop({
indeterminate: true,
checked: false
});

}

}

checkSiblings(container);
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: