Javascript 如何在jsTree中获取所选节点的id?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2585502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the id of the selected node in jsTree?
提问by murze
采纳答案by harpo
Nodes in jsTree are essentially wrapped list items. This will get you a reference to the first one.
jsTree 中的节点本质上是包装的列表项。这将为您提供对第一个的参考。
var n = $.tree.focused().get_node('li:eq(0)')
You can replace $.tree.focused()if you have a reference to the tree.
$.tree.focused()如果您有对树的引用,则可以替换。
To get the id, take the first matched element
要获取 id,取第一个匹配的元素
if (n.length)
id = n[0].id
or you can use the jQuery attr function, which works on the first element in the set
或者您可以使用 jQuery attr 函数,它适用于集合中的第一个元素
id = n.attr('id');
回答by Brad
Unable to get harpo's solution to work, and unwilling to use Olivier's solution as it uses internal jsTree functions, I came up with a different approach.
无法让 harpo 的解决方案起作用,也不愿意使用 Olivier 的解决方案,因为它使用内部 jsTree 函数,我想出了一个不同的方法。
$('#tree').jstree('get_selected').attr('id')
It's that simple. The get_selectedfunction returns an array of selected list items. If you do .attron that array, jQuery will look at the first item in the list. If you need IDs of multiple selections, then treat it as an array instead.
就这么简单。该get_selected函数返回选定列表项的数组。如果您.attr在该数组上执行此操作,jQuery 将查看列表中的第一项。如果您需要多个选择的 ID,则将其视为数组。
回答by tipycalFlow
In jstreeversion 3.1.1, you can get it directly from get_selected:
在jstreeversion 中3.1.1,您可以直接从get_selected:
$("#<your tree container's id>").jstree("get_selected")
回答by Peter Rankin
In the most recent version of jsTree (checked at 3.3.3), you can do this to get an array of IDs:
在最新版本的 jsTree(在 3.3.3 中检查)中,您可以这样做以获取 ID 数组:
var ids = $('#tree').jstree('get_selected');
This will return, for example, ["selected_id1", "selected_id2", "selected_id3"]. If you want to get the selected nodes(not IDs), you can do this:
例如,这将返回["selected_id1", "selected_id2", "selected_id3"]. 如果要获取选定的节点(而不是 ID),可以执行以下操作:
var nodes = $('#tree').jstree('get_selected', true);
The current docscontain more information.
在当前文档包含更多信息。
回答by Olivier Grimard
$.jstree._reference('#my_tree_container')._get_node(null, true).each(function() {
id = $(this).attr("id");
alert('Id selected: ' + id);
});
回答by agarcia
I was having problems getting the selected ids from a tree with MULTIPLE selections. This is the way I got them:
我在从具有多个选择的树中获取选定的 ID 时遇到问题。这是我得到它们的方式:
var checked_ids = [];
$("#your-tree-id").jstree('get_selected').each(function(){
checked_ids.push($(this).data('id'));
});
回答by Damien C
In my case, the data call doesnt work. I succeed in accessing my node data by using attr function.
就我而言,数据调用不起作用。我使用 attr 函数成功访问了我的节点数据。
$("#tree").jstree("get_selected").attr("my-data-name");
回答by Ali
to get all selected ids use the below code
要获取所有选定的 ID,请使用以下代码
var selectedData = [];
var selectedIndexes;
selectedIndexes = $("#jstree").jstree("get_selected", true);
jQuery.each(selectedIndexes, function (index, value) {
selectedData.push(selectedIndexes[index].id);
});
now you have all the selected id's in the "selectedData" variable
现在您在“selectedData”变量中拥有所有选定的 id
回答by Tom McDonough
These are all old answers for old versions. As of version 3.3.3 this will work to get all attributes of the selected node.
这些都是旧版本的旧答案。从 3.3.3 版本开始,这将用于获取所选节点的所有属性。
$('#jstree').jstree().get_selected(true)[0]
If you then want the id then add .id at the end. You can look at all the other attributes in web developer tools if you copy the above code.
如果您想要 id,请在最后添加 .id。如果您复制上述代码,您可以在 Web 开发人员工具中查看所有其他属性。
回答by Arti Gaikwad
You can use the following code var nodes = $("#jstree_demo_div").jstree(true).get_selected("full", true);//List of selected node
可以使用如下代码 var nodes = $("#jstree_demo_div").jstree(true).get_selected("full", true);//选中节点列表
nodes[0].id//Which will give id of 1st object from array
nodes[0].id//从数组中给出第一个对象的id

