获取选定的 Jstree 节点值 JQuery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25481538/
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
Get Selected Jstree node values JQuery
提问by Jakub Wisniewski
Hi i have problem with getting data of selected node of jstree model.
嗨,我在获取 jstree 模型选定节点的数据时遇到问题。
<script type="text/javascript">
$('#preview').on("changed.jstree", function (e, data) {
console.log(data.selected);
console.log(data.selected.attr("text"));
});
</script>
the first console log shows me "[js1_1]" or "[js1_2]" depending on the selected node. But The second log "undefined is not a function" ;/ I have tried many different ways but i failed to achieve getting node text(title) or any other info
第一个控制台日志根据所选节点显示“[js1_1]”或“[js1_2]”。但是第二个日志“未定义不是函数”;/我尝试了很多不同的方法,但未能获得节点文本(标题)或任何其他信息
I send list of models as json and it looks like this:
我将模型列表作为 json 发送,它看起来像这样:
Public Class JsTreeModel
Public Property text As String
Public Property icon As String
Public Property Id As String
Public Property PId As String
Public Property ParentId As String
Public Property Status As Integer
End Class
anyone has solution ?
有人有解决方案吗?
UPDATE jstree code
更新jstree代码
<script type="text/javascript">
$(document).ready(function () {
$('#WhenRemoving').toggle($('.RemoveCheckbox').is(":checked"));
$('#WhenAdding').toggle(!$('.RemoveCheckbox').is(":checked"));
$('#preview').jstree({
'core': {
'data': {
'url': '/TreeTest/TreePreview/',
'data': function (node) {
return node;
}
}
}
}).bind("loaded.jstree", function (event, data) {
$(this).jstree("open_all");
})
});
</script>
when i add this to my jstree i can see selected name node in html div
当我将此添加到我的 jstree 时,我可以在 html div 中看到选定的名称节点
.on('changed.jstree', function (e, data) {
var i, j, r = [];
for (i = 0, j = data.selected.length; i < j; i++) {
r.push(data.instance.get_node(data.selected[i]).text);
}
$('#event_result').html('Selected: ' + r.join(', '));
})
});
采纳答案by Kartikeya Khosla
instead of
代替
data.selected.attr("text")
try this
尝试这个
data.selected.text()
You can also get selected jstree
node text with this :
您还可以通过以下方式获取选定的jstree
节点文本:
console.log($("#preview").jstree("get_selected").text());
OR just bind select_node.jstree
as shown
或者只是select_node.jstree
如图所示绑定
.bind("select_node.jstree", function (NODE, REF_NODE) {
var a = $.jstree._focused().get_selected();
}
回答by Strabek
None above worked for me. This worked:
以上都不适合我。这有效:
$(treeID).jstree().get_selected(true)[0].text;
回答by Ali
simple 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 Banshee93
Add plugin:
"plugins": ["changed"]
Code:
$('#container').on('changed.jstree',function(e,data){ alert(data.node.text); });
添加插件:
"plugins": ["changed"]
代码:
$('#container').on('changed.jstree',function(e,data){ alert(data.node.text); });
回答by BonSai Chinese
oh.. it's very easy instead of
哦..这很容易,而不是
console.log(data.instance.get_node(data.selected[0]).text)
console.log(data.instance.get_node(data.selected[0]).text)