获取jqueryjstree中的Checked元素
我已经用JSON对象创建了一个Jquery jstree。我的树长得很好 创建jstree获取jqueryjstree中的Checked元素,jquery,jstree,Jquery,Jstree,我已经用JSON对象创建了一个Jquery jstree。我的树长得很好 创建jstree $("#tree").jstree({ "json_data": { "data": [{ "data": "pe_opensourcescanning", "id": 0, "pId": -1, "children": [{ "data": "tags"
$("#tree").jstree({
"json_data": {
"data": [{
"data": "pe_opensourcescanning",
"id": 0,
"pId": -1,
"children": [{
"data": "tags",
"id": 30,
"pid": 0
}, {
"data": "branches",
"id": 29,
"pid": 0
}, {
"data": "trunk",
"id": 1,
"pid": 0,
"children": [{
"data": "import-export",
"id": 28,
"pid": 1
}, {
"data": "custom_development",
"id": 12,
"pid": 1
}, {
"data": "Connectors",
"id": 7,
"pid": 1
}, {
"data": "support",
"id": 6,
"pid": 1
}, {
"data": "Installation-Configuration",
"id": 5,
"pid": 1
}, {
"data": "backup",
"id": 2,
"pid": 1
}]
}]
}]
},
"plugins": ["themes", "json_data", "checkbox", "ui"]
}).bind("select_node.jstree", function (e, data) {
alert(data.rslt.obj.data("id"));
});
现在我需要每个选中节点的“id”和“data”值。我试着写些东西,但不幸的是,那不起作用。请帮助我如何实现这个目标
获取检查节点
$("#tree").jstree("get_checked",null,true).each(function () {
alert(this.data);
alert(this.id);
});
您获取检查节点的功能是正确的,问题是您的JSON格式不正确;设置树上的属性,以便无法从方法中获取它们 您必须使用属性
attr
,否则节点上不会从文档中设置任何数据:
attr对象将作为属性显示在生成的li节点上
代码:
演示:
文档:查看这个链接,我已经看到了。但是在我的例子中,“this.id”和“this.data”将作为void.try console.log(this)出现;这会帮你找到正确的方向direction@Rooster我已经按照你的建议试过了。它在控制台中显示为“
$("#tree").jstree({
"json_data": {
"data": [{
"data": "pe_opensourcescanning",
"attr": {
"id": 77,
"pId": -1
},
"children": [{
"data": "tags",
"attr": {
"id": 30,
"pid": 0
}
}, {
"data": {
"title": "branches"
},
"attr": {
"id": 29,
"pid": 0
}
}]
}]
},
"plugins": ["themes", "json_data", "checkbox", "ui"]
})
$("#getChecked").click(function () {
$("#tree").jstree("get_checked", null, true).each(function (i, e) {
console.log($(this).attr("id"))
});
});