试图在jstree中的change_state上获取已检查项目的列表

使用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

返回’ colors ‘。 我在data对象周围钓鱼,但没有在那里找到它(也许我错过了它?)

为了获取已检查节点的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 +文本对象,您可以将它们发送到服务器。