抄自 http://tieba.baidu.com/p/4394654036
$(document).ready(function () { var ID=@ViewBag.id; $.ajax({ type: "GET", url: "/sys/menu/GetAll", contentType: "application/json; charset=utf-8", dataType: "json", success: function (data1) { $.fn.zTree.init($("#treeDemo"), setting, data1); $.fn.zTree.getZTreeObj("treeDemo").expandAll(true); var nodes =$.fn.zTree.getZTreeObj("treeDemo").getNodes(); var array =$.fn.zTree.getZTreeObj("treeDemo").transformToArray(nodes); $.ajax({ url: "/Sys/Role/GetRoleMenus/", type: "POST", data:{id:ID}, dataType: "json", success: function (hastree) { for (var i in array) { for (var j in hastree) { // alert(hastree[j].MenuID); if (!array[i].isParent) { if (array[i].MenuID == hastree[j].MenuID) { $.fn.zTree.getZTreeObj("treeDemo").checkNode(array[i], true, true); } } } } } }); }, error: function (error) { alert(error.status + "<--and--> " + error.statusText); } }); });
获得以选id
function count() { var zTree = $.fn.zTree.getZTreeObj("treeDemo"); checkCount = zTree.getCheckedNodes(true); var classpurview = ""; for(var i=0;i<checkCount.length;i++) { if(i==0) { classpurview = checkCount[i].MenuID; } else { classpurview += "," + checkCount[i].MenuID; } } //alert(classpurview); $("#checklist").val(classpurview); }