/**
* 全选和不全选复选框,可适用于多级选择
*/
// check child
var _checkedChild = function (_self) {
var _tag = "";
var self = $("input[self=" + _self + "]");
self.each(function (i, data) {
_tag += this.checked;
});
if (_tag.indexOf("false") < 0) {
var _f = true;
var _child = $("input[child=" + _self + "]");
_child.each(function (i, e) {
this.checked = true;
if($(this).attr("self") != undefined){
_checkedChild($(this).attr("self"));
}
});
}
}
// uncheck child
var _uncheckedChild = function (child) {
var _f = true;
var self = $("input[child=" + child + "]");
self.each(function(i,data){
this.checked = false;
if($(this).attr("self") != undefined){
_uncheckedChild($(this).attr("self"));
}
});
};
// uncheck self
var _uncheckedSelf = function (self) {
var self = $("input[self=" + self + "]");
self.each(function (i, data) {
data.checked = false;
if ($(this).attr("child") != undefined) {
_uncheckedSelf($(this).attr("child"));
}
});
};
// check self
var _checkedSelf = function (self) {
var self = $("input[self=" + self + "]");
self.each(function (i, data) {
this.checked = true;
if ($(this).attr("child") != undefined) {
_checkedSelf($(this).attr("child"));
}
});
};
$("table.listTable").on("change","input[self]",function () {
if (this.checked) {
_checkedChild($(this).attr("self"));
} else {
_uncheckedChild($(this).attr("self"));
}
});
$("table.listTable").on("change","input[child]",function () {
if (!this.checked) {
_uncheckedSelf($(this).attr("child"));
} else {
_checkedSelf($(this).attr("child"));
}
});
<input class="checkbox" type="checkbox" name="" id="checkAll" value="" child="self" />
<input class='checkbox' type='checkbox' name='' id='' value='' self='self' />