问候,我正在使用jsTree来生成我的分层数据.JsTree生成如下:
$(function() {
$("#industries").tree({
data: {
type: "json",
opts: {
url: "/Admin/GetIndustries/"
}
}
});
});
Run Code Online (Sandbox Code Playgroud)
它的工作原理和jsonresult是这样的:
[{"attributes":[],"data":{"title":"Adwokaci, Notariusze","id":"1a051101-c3fa-48f2-b2e1-c60d1b67ea22"},"children":[{"attributes":[],"data":{"title":"Kancelarie adwokackie","id":"26d6cff1-3c7f-4a2f-bf5a-422e08127b43"
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何在某些隐藏字段中保存所选节点的ID?我做这样的事情:
<script type="text/javascript">
$("#industries").click(function() {
var tree = $.tree.reference("industries");
var t = $.tree.focused(); if (t.selected) t.selected; else alert("Select a node first");
alert(t.id);
});
Run Code Online (Sandbox Code Playgroud)
但它不起作用.我进入警报窗口"未定义".有人可以帮帮我吗?
编辑: 我已经更改了jstree实例,如下所示:
$(function() {
$("#industries").tree({
callback: {
onselect: function(NODE, TREE_OBJ) {
alert(NODE.id);
}
},
data: {
type: "json",
opts: {
url: "/Admin/GetIndustries/"
}
}
});
});
Run Code Online (Sandbox Code Playgroud)
我得到空洞的警觉
jlr*_*lin 17
或者只是绑定选择节点:
$("#industries").tree({
callback: {
onselect: function(NODE, TREE_OBJ) {
alert(NODE.id);
}
},
data: {
type: "json",
opts: {
url: "/Admin/GetIndustries/"
}
}
})
.bind("select_node.jstree", function (NODE, REF_NODE) {
var a = $.jstree._focused().get_selected();
}
});
Run Code Online (Sandbox Code Playgroud)
尝试查看ID或NODE的变量a.我实际上是在使用REF_NODE来获取
我没有检查所有的答案,但看到你没有选择任何,所以决定发布一个适合我的方法
$(function () {
$("#demo1")
.bind("select_node.jstree", function (event, data) {
var selectedObj = data.rslt.obj;
alert(selectedObj.attr("id") + selectedObj.attr("data"));
})
Run Code Online (Sandbox Code Playgroud)
这是你想要节点的id和标题.希望这可以帮助