树形菜单复选框级联选择

点击复选框后,设置父级、子级的选中状态。

全部JS代码如下:

function getSiblings(id){
	var tr = $("#"+id);
	var pId = tr.attr("pId");
	var siblings = [];
	if(pId != undefined){
		$("tr").each(function(i, ele) {
			tr = $(ele);
			if(tr.attr("pId") == pId){
				siblings.push($(this));
			}
		});
	}
	return siblings;
}
function getParent(id){
	var tr = $("#"+id);
	var parents = [];
	parents.push(tr);

	var pId = tr.attr("pId");
	if(pId != undefined){parents = parents.concat(getParent(pId));}
	return parents;
}		
function getChildren(id){
	var tr = null;
	var children = [];
	if(id != undefined){
		$("tr").each(function(i, ele) {
			tr = $(ele);
			if(tr.attr("pId") == id){
				children.push(tr);
				children = children.concat(getChildren(tr.find("input:eq(0)").val()));
			}
		});
	}
	return children;
}
function chkModel(obj) {
	var id = obj.value, chkbox = null;
	$.each(getChildren(id), function(i,v){
		v.find("input:eq(0)").attr("checked", function(){return obj.checked ? "checked":""});
	});
	$.each(getParent(id), function(i,v){
		if(i > 0){
			chkbox = v.find("input:eq(0)");
			chkbox.attr("checked", function(){return getChildrenChecked(chkbox.val()) ? "checked":""});
		}
	});
}
function getChildrenChecked(id){
	var chked = false;
	$.each(getChildren(id), function(i,v){
		chked = v.find("input:eq(0)").attr("checked") == true;
		if(chked) {return false;}
	});
	return chked;
}

 

你可能感兴趣的:(function,null,input)