我有一棵树,我采用了zTree的样本形式,但是我无法从这棵树中获取选定的值。尽管我已经更改了原始代码,但是在这里我发布了官方Ztree Demo的原始代码。任何帮助!!

<SCRIPT type="text/javascript">
    var setting = {
        data: {
            simpleData: {
                enable: true
            }
        }
    };


数组来存储树的节点

    var zNodes =[
        { id:1, pId:0, name:"pNode 1", open:true},
        { id:11, pId:1, name:"pNode 11"},
        { id:111, pId:11, name:"leaf node 111"},
        { id:12, pId:1, name:"pNode 12"},
        { id:121, pId:12, name:"leaf node 121"},
        { id:13, pId:1, name:"pNode 13 - no child", isParent:true},
        { id:2, pId:0, name:"pNode 2"},
        { id:21, pId:2, name:"pNode 21", open:true},
        { id:211, pId:21, name:"leaf node 211"},
        { id:22, pId:2, name:"pNode 22"},
        { id:221, pId:22, name:"leaf node 221"},
        { id:23, pId:2, name:"pNode 23"},
        { id:231, pId:23, name:"leaf node 231"},
        { id:3, pId:0, name:"pNode 3 - no child", isParent:true}
    ];


该数组的树初始化

    $(document).ready(function(){
        $.fn.zTree.init($("#treeDemo"), setting, zNodes);
    });
</SCRIPT>


在此,树以id treeDemo在Div中显示。在这里,我想从此树中提醒警报框中的选定值。

<BODY>
     <div class="content_wrap">
          <div class="zTreeDemoBackground left">
               <ul id="treeDemo" class="ztree"></ul>
          </div>
     </div>
</BODY>

最佳答案

在zTree v3.x上,
您需要两件事:

1)在设置中,添加一个回调

var setting = {
        ...
        callback: {
            onCheck: zTreeOnCheck
        }
    };


2)在回调中使用ztreeOnCheck,您可以通过每次检查/取消选中所有选定的节点来获得。样品

function zTreeOnCheck(event, treeId, treeNode) {
  var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
  var nodes = treeObj.getCheckedNodes(true);
  console.log(nodes); //Here are all of the selected nodes
};

10-06 00:49