日期:2014-05-16 浏览次数:20950 次
function getParentId(id){
var arr = new Array();
$.each(json, function(i,item){
if(item.Id==id && item.ParentId!=0){
arr.push(item.ParentId);
arr = arr.concat(getParentId(item.ParentId));
}
});
return arr;
}
<script>
var s = eval([{ "Id": 1, "Name": "机构1", "ParentId": 0 }, { "Id": 4, "Name": "机构11", "ParentId": 1 }, { "Id": 2, "Name": "机构2", "ParentId": 1 }, { "Id": 3, "Name": "机构3", "ParentId": 2 }, { "Id": 21, "Name": "机构111", "ParentId": 4}]);
p = GetParentId(21);
q = GetParentId(p);
alert(p + ":" + q);
function GetParentId(id) {
var n = "";