获取jstree的已检查节点ID列表[重复]

2024-02-08

我是新来的jstree and jQuery并且在我的测试树中进行节点检查时遇到了一些问题。

用户首先应勾选自己需要的节点,然后点击“概括”按钮以获取警报窗口中已检查节点的 ID 列表。我还想导出 ID 列表以供进一步使用cgi file.

这是我的代码:

<!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。您应该使用 every() 来填充数组:

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'));
});

我创建了一个jsfiddlehttp://jsfiddle.net/J5ZaK/123/ http://jsfiddle.net/J5ZaK/123/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

获取jstree的已检查节点ID列表[重复] 的相关文章