問題描述
使用 jsTree (pre1.0_fix_1),我想獲取所有選中項目的 id
列表(或者更好的是,一個帶有 id
AND 每個選中項的文本).然后我會用這個進行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'));
});
});
這只是從容器的id
返回'colors':<div id="colors">
.我在 data
對象周圍摸索了一下,但沒有在其中找到它(也許我錯過了它?)
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+text 對象,您可以將其發送到服務器.
After that checked_json
will contain an array of of id+text objects which you could send to the server.
這篇關于試圖在 jstree 中的 change_state 上獲取已檢查項目的列表的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!