试图在jstree中的change_state上获取已检查项目的列表 [英] Trying to get a list of checked items on change_state in jstree

查看:181
本文介绍了试图在jstree中的change_state上获取已检查项目的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 jsTree (pre1.0_fix_1),我想获取所有已检查项目的 id 列表(或更好) ,一个带有 id 的JSON对象和每个已检查项目的文本。然后我会用这个做一个ajax调用。此外,这应该在任何时候进行状态更改时检查或取消选中。

Using jsTree (pre1.0_fix_1), I would like to obtain a list of id's for all the checked items (or better, a JSON object with id AND text of each checked item). I will then make an ajax call with this. Additionally, this should happen any time there is a state change of something getting checked or unchecked.

这是我目前所拥有的:

$(function(){

  n = $('#colors').jstree({
    "plugins": ["themes", "html_data", "checkbox"]
  }).bind("change_state.jstree", function(e, data){
  console.log($('#colors').jstree('get_selected').attr('id'));
  });
});

这只是从容器的<$ c返回' colors ' $ c> id :< div id =colors> 。我在数据对象附近钓鱼,但没有在那里找到它(也许我错过了它?)

This is just returning 'colors', from the container'sid: <div id="colors">. I fished around the data object, but didn't find it in there (perhaps I missed it?)

推荐答案

为了获得已检查节点的JSON,你应该使用 get_checked 而不是 get_selected 。试试这个:

In order to get the checked nodes' JSON you should be using get_checked and not get_selected. Try this:

    var checked = $("#colors").jstree("get_checked",null,true);
    var checked_json = [];
    $(checked).each(function (i,node) {
        var id = $(node).attr("id");
        var text = $(node).attr("text");
        var node_json = { 
            "id" : id, 
            "text" : text 
        };
        checked_json.push(node_json);
    });

之后 checked_json 将包含一个数组您可以发送到服务器的id +文本对象。

After that checked_json will contain an array of of id+text objects which you could send to the server.

这篇关于试图在jstree中的change_state上获取已检查项目的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆