This question already has answers here:
How do I get the id of the selected node in jsTree?
(14个回答)
6年前关闭。
我是jstree和jQuery的新手,在测试树中检查节点时遇到了一些问题。
用户应首先检查所需节点,然后单击“摘要”按钮以在警报窗口中获取已检查节点的ID列表。我还想导出ID列表以在cgi文件中进一步使用。
这是我的代码:
有什么建议我该怎么做?
更新1:
我尝试从另一篇文章中使用Brad的解决方案,但仍然无法正常工作,因为我不知道如何将ID添加到数组中。
这是我的新代码:
任何帮助,将不胜感激。
更新2:
数组仍然无法填充:
UPD:
所选项目是您选择/突出显示的项目。您需要选中的项目。所以代码将是
我创建了一个jsfiddle http://jsfiddle.net/J5ZaK/123/
(14个回答)
6年前关闭。
我是jstree和jQuery的新手,在测试树中检查节点时遇到了一些问题。
用户应首先检查所需节点,然后单击“摘要”按钮以在警报窗口中获取已检查节点的ID列表。我还想导出ID列表以在cgi文件中进一步使用。
这是我的代码:
<!doctype html>
<html>
<head>
<title>jstree test</title>
</head>
<body>
<div id="testtree">
<ul>
<li id="1" title="ID:1"><a>Fruits and Vegetables</a>
<ul>
<li id="11" title="ID:11"><a>Fruit</a>
<ul>
<li id="111" title="ID:111"><a>Apple</a></li>
<li id="112" title="ID:112"><a>Banana</a></li>
</ul>
</li>
<li id="12" title="ID:12"><a>Vegetables</a>
<ul>
<li id="121" title="ID:121"><a>Zucchini</a></li>
<li id="122" title="ID:122"><a>Tomato</a>
<ul>
<li id="1221" title="ID:1221"><a>Red Tomato</a></li>
<li id="1222" title="ID:1222"><a>Green Tomato</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<button>Summary</button>
<script type="text/javascript" src="_lib/jquery.js"></script>
<script type="text/javascript" src="jquery.jstree.js"></script>
<script>
$(document).ready(function() {
var checked_ids = []; // list for the checked IDs
$("#testtree").jstree({
"plugins" : [ "themes", "html_data", "checkbox", "ui" ]
});
})
var button = document.querySelector( "button" );
// If the user clicks on the button, show him the IDs of the checked fruits/vegetables:
button.addEventListener( "click", function( ev ) {
alert( "Your Selection: ");
}, false);
</script>
</body>
</html>
有什么建议我该怎么做?
更新1:
我尝试从另一篇文章中使用Brad的解决方案,但仍然无法正常工作,因为我不知道如何将ID添加到数组中。
这是我的新代码:
<script>
var checked_ids = [];
$(document).ready(function() {
$("#testtree").jstree({
"plugins" : [ "themes", "html_data", "checkbox", "ui" ]
});
// How do I add the IDs to the array?
$("#testtree").jstree('get_selected').attr('id')
})
var button = document.querySelector( "button" );
// If the user clicks on the button, show him the IDs of the checked fruits/vegetables:
button.addEventListener( "click", function( ev ) {
alert("Your Selection: "+ checked_ids.join("\n"));
}, false);
</script>
任何帮助,将不胜感激。
更新2:
数组仍然无法填充:
<button>Summary</button>
<script type="text/javascript" src="_lib/jquery.js"></script>
<script type="text/javascript" src="jquery.jstree.js"></script>
<script>
$(document).ready(function() {
$("#testtree").jstree({
"plugins" : [ "themes", "html_data", "checkbox", "ui" ]
});
var arr = new Array();
$("#testtree").jstree('get_checked').each(function(index) {
arr.push($(this).attr('id'));
});
var button = document.querySelector( "button" );
button.addEventListener( "click", function( ev ) {
alert("Your Selection: "+ arr.length + " " + arr[0] + " " + arr[1]);
}, false);
})
</script>
最佳答案
问题出在这个地方.attr('id')。它仅返回第一个ID。您应该使用each()来填充数组:
var arr = new Array();
$("#testtree").jstree('get_selected').each(function(index) {
arr.push($(this).attr('id'));
});
UPD:
所选项目是您选择/突出显示的项目。您需要选中的项目。所以代码将是
var arr = new Array();
$("#testtree").jstree('get_checked').each(function(index) {
arr.push($(this).attr('id'));
});
我创建了一个jsfiddle http://jsfiddle.net/J5ZaK/123/
关于jquery - 获取jstree的已检查节点ID列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18355420/