Javascript 如何获取jsTree中所选节点的id?
我如何才能在网络中获取所选节点的idJavascript 如何获取jsTree中所选节点的id?,javascript,jquery,jstree,Javascript,Jquery,Jstree,我如何才能在网络中获取所选节点的id jsTree中的节点本质上是包装的列表项。这将为您提供对第一个的引用 var n = $.tree.focused().get_node('li:eq(0)') 如果有对树的引用,则可以替换$.tree.focused() 要获取id,请获取第一个匹配的元素 if (n.length) id = n[0].id 或者可以使用jQuery attr函数,该函数作用于集合中的第一个元素 id = n.attr('id'); 无法让harpo的解决方
jsTree中的节点本质上是包装的列表项。这将为您提供对第一个的引用
var n = $.tree.focused().get_node('li:eq(0)')
如果有对树的引用,则可以替换$.tree.focused()
要获取id,请获取第一个匹配的元素
if (n.length)
id = n[0].id
或者可以使用jQuery attr函数,该函数作用于集合中的第一个元素
id = n.attr('id');
无法让harpo的解决方案发挥作用,也不愿意使用Olivier的解决方案,因为它使用内部jsTree函数,我想出了一种不同的方法
$('#tree').jstree('get_selected').attr('id')
就这么简单。
get_selected
函数返回所选列表项的数组。如果对该数组执行.attr
,jQuery将查看列表中的第一项。如果需要多个选择的ID,则将其视为数组。使用最新版本的Jstree;您可以按以下方式执行此操作:
<script type="text/javascript>
var checked_ids = [];
$('#your-tree-id).jstree("get_checked",null,true).each(function(){
checked_ids.push(this.id);
});
alert(checked_ids.join(","));
</script>
我从具有多个选择的树中获取所选ID时遇到问题。这就是我得到它们的方式:
var checked_ids = [];
$("#your-tree-id").jstree('get_selected').each(function(){
checked_ids.push($(this).data('id'));
});
在我的例子中,数据调用不起作用。
我使用attr函数成功地访问了我的节点数据
$("#tree").jstree("get_selected").attr("my-data-name");
在jstree
版本3.1.1
中,您可以直接从get\u selected
获取它:
$("#<your tree container's id>").jstree("get_selected")
$(“#只需使用
var nodeId = $('#FaqTreeView').jstree().get_selected("id")[0].id;
其中#FaqTreeView
是包含jstree的div的id。在某些情况下和/或jstree版本中,此解决方案不起作用
$('#tree').jstree('get_selected').attr('id');
除了定义的“id”,我什么也得不到。
我的诀窍是:
$("#tree").jstree("get_selected").toString();
在最新版本的jsTree(在3.3.3中检查)中,您可以这样做来获取ID数组:
var ids = $('#tree').jstree('get_selected');
例如,这将返回[“selected_id1”、“selected_id2”、“selected_id3”]
。如果要获取所选节点(而不是ID),可以执行以下操作:
var nodes = $('#tree').jstree('get_selected', true);
包含更多信息。这些都是旧版本的旧答案。从版本3.3.3开始,这将用于获取所选节点的所有属性
$('#jstree').jstree().get_selected(true)[0]
如果需要id,请在末尾添加.id。如果复制上述代码,您可以查看web developer工具中的所有其他属性。您可以使用以下代码
var nodes=$(“#jstree_demo_div”).jstree(true).get_selected(“full”,true);//所选节点的列表
节点[0].id//将给出数组中第一个对象的id以获取所有选定id,请使用以下代码
var selectedData = [];
var selectedIndexes;
selectedIndexes = $("#jstree").jstree("get_selected", true);
jQuery.each(selectedIndexes, function (index, value) {
selectedData.push(selectedIndexes[index].id);
});
现在您在“selectedData”变量中有了所有选定的id不知道为什么data.rslt.obj.attr(“id”)在这里不起作用(如果您在onselect处理程序中有数据作为参数),但这样做了,并完成了对解决方案的>1h搜索-谢谢!绝对是正确的解决方案。我认为这不起作用,因为。attr
未定义。这是否是因为未加载jQuery?$(“#tree”).jstree('get#u selected'))
确实返回了一个所选节点的id数组。正如mmcrae提到的,id不执行任务。我的窍门是$(“#tree”).jstree('get#selected').toString();在最新版本的jstree中,代码$('#tree').jstree('get#selected'))
实际上返回的是一个ID数组,而不是节点数组。要获取节点数组,请使用$(“#tree”).jstree('get#u selected',true)
。上述答案在最新版本的jsTree中不起作用。有关详细信息,请参阅。您的答案是100%正确的。对于其他方法,“get_selected”方法不会获取节点,而是获取节点的id。您可以通过调用方法“get_node”-ex.var node=$(“#myelement”).jsTree(“获取节点(nodeId);我不知道获取实际节点的参数'true'!
var nodes = $('#tree').jstree('get_selected', true);
$('#jstree').jstree().get_selected(true)[0]
var selectedData = [];
var selectedIndexes;
selectedIndexes = $("#jstree").jstree("get_selected", true);
jQuery.each(selectedIndexes, function (index, value) {
selectedData.push(selectedIndexes[index].id);
});