【问题标题】:Trying to get a list of checked items on change_state in jstree试图在 jstree 中的 change_state 上获取已检查项目的列表
【发布时间】:2012-03-03 09:01:49
【问题描述】:

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

这是我目前拥有的:

$(function(){

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

这只是从容器的id:<div id="colors"> 返回“colors”。我在data 对象周围钓鱼,但没有在其中找到它(也许我错过了它?)

【问题讨论】:

    标签: javascript jquery jquery-plugins jstree


    【解决方案1】:

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

        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+text 对象,您可以将其发送到服务器。

    【讨论】:

    • 非常有用的感谢 - 虽然有几个拼写错误 - "id" : id _id,应该只是 "id" : id,,最后一行应该是 });
    • 已修复。谢谢,由于某种原因我没有注意到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    • 2015-05-28
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    相关资源
    最近更新 更多