Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取jqueryjstree中的Checked元素_Jquery_Jstree - Fatal编程技术网

获取jqueryjstree中的Checked元素

获取jqueryjstree中的Checked元素,jquery,jstree,Jquery,Jstree,我已经用JSON对象创建了一个Jquery jstree。我的树长得很好 创建jstree $("#tree").jstree({ "json_data": { "data": [{ "data": "pe_opensourcescanning", "id": 0, "pId": -1, "children": [{ "data": "tags"

我已经用JSON对象创建了一个Jquery jstree。我的树长得很好

创建jstree

$("#tree").jstree({
    "json_data": {
        "data": [{
            "data": "pe_opensourcescanning",
            "id": 0,
            "pId": -1,
            "children": [{
                "data": "tags",
                "id": 30,
                "pid": 0
            }, {
                "data": "branches",
                "id": 29,
                "pid": 0
            }, {
                "data": "trunk",
                "id": 1,
                "pid": 0,
                "children": [{
                    "data": "import-export",
                    "id": 28,
                    "pid": 1
                }, {
                    "data": "custom_development",
                    "id": 12,
                    "pid": 1
                }, {
                    "data": "Connectors",
                    "id": 7,
                    "pid": 1
                }, {
                    "data": "support",
                    "id": 6,
                    "pid": 1
                }, {
                    "data": "Installation-Configuration",
                    "id": 5,
                    "pid": 1
                }, {
                    "data": "backup",
                    "id": 2,
                    "pid": 1
                }]
            }]
        }]
    },
    "plugins": ["themes", "json_data", "checkbox", "ui"]
}).bind("select_node.jstree", function (e, data) {
    alert(data.rslt.obj.data("id"));
});
现在我需要每个选中节点的“id”和“data”值。我试着写些东西,但不幸的是,那不起作用。请帮助我如何实现这个目标

获取检查节点

$("#tree").jstree("get_checked",null,true).each(function () { 
    alert(this.data);
    alert(this.id);
});

您获取检查节点的功能是正确的,问题是您的JSON格式不正确;设置树上的属性,以便无法从方法中获取它们

您必须使用属性
attr
,否则节点上不会从文档中设置任何数据:

attr对象将作为属性显示在生成的li节点上

代码:

演示:


文档:

查看这个链接,我已经看到了。但是在我的例子中,“this.id”和“this.data”将作为void.try console.log(this)出现;这会帮你找到正确的方向direction@Rooster我已经按照你的建议试过了。它在控制台中显示为“
  • ”。这是什么意思。我的代码中的问题在哪里。请帮助我。jstree模拟复选框,除非您将参数设置为告诉它使用真实的复选框。也许你有。您可以尝试使用console.log($(this.find('input[type=“checkbox”]);如果它的长度是1,只需要从中获取id和数据,如果它是0,那么您需要获取id和数据,它是从哪个位置存储的,与li元素有关。非常感谢您的宝贵建议。这个问题现在已经解决了。
    $("#tree").jstree({
        "json_data": {
            "data": [{
                "data": "pe_opensourcescanning",
                    "attr": {
                    "id": 77,
                        "pId": -1
                },
                    "children": [{
                    "data": "tags",
                        "attr": {
                        "id": 30,
                            "pid": 0
                    }
                }, {
                    "data": {
                        "title": "branches"
                    },
                        "attr": {
                        "id": 29,
                            "pid": 0
                    }
                }]
            }]
        },
            "plugins": ["themes", "json_data", "checkbox", "ui"]
    })
    
    $("#getChecked").click(function () {
        $("#tree").jstree("get_checked", null, true).each(function (i, e) {
            console.log($(this).attr("id"))
        });
    });