如何获取jstree JSON数据中的data-id
How to get data-id in jstree JSON data
我正在尝试从使用 JsTree(复选框表单)生成的树形表单中获取 data-id
,但它不起作用。我使用 $.each()
,因为您可以选中更多复选框。这是我的脚本的样子:
$(document).ready(function() {
$(".js-save-button").on('click', function(){
let boxes = check_checkboxes();
alert(JSON.stringify(boxes));
});
function check_checkboxes(){
let boxes_id = [];
let boxes = $(".js-categories-checkboxtree").jstree("get_checked", true);
$.each(boxes, function() {
//let id = jQuery(this); //getting info
//let id = (data.node.id); //not working
let id = $(this).data('id'); //not working
boxes_id.push(id);
});
return boxes_id;
}
});
我可以简单地得到这个(但它 returns 不是我需要的我可以验证我在 this
中有正确的元素):
let id = $(this).attr('id');
但是我无法得到这个(它 returns null)
let id = $(this).data('id');
我试图只提醒我所知道的关于该元素的一切:
let id = jQuery(this)
我可以看到有很多数据属性,但我无法获得其中任何一个...请问有什么想法吗?
所以我终于找到了解决办法!其实很简单!您必须使用 .map()
!
访问这些 data
像这样:
$(".js-save-product").on('click', function(){
let boxes = check_checkboxes();
alert(JSON.stringify(boxes));
});
function check_checkboxes(){
let boxes_id = [];
let boxes = $('.js-categories-checkboxtree').jstree('get_checked', true)
$.each(boxes, function() {
let id = $(this).map(function() { return this.data.id }).get().join();
boxes_id.push(id);
});
return boxes_id;
}
我正在尝试从使用 JsTree(复选框表单)生成的树形表单中获取 data-id
,但它不起作用。我使用 $.each()
,因为您可以选中更多复选框。这是我的脚本的样子:
$(document).ready(function() {
$(".js-save-button").on('click', function(){
let boxes = check_checkboxes();
alert(JSON.stringify(boxes));
});
function check_checkboxes(){
let boxes_id = [];
let boxes = $(".js-categories-checkboxtree").jstree("get_checked", true);
$.each(boxes, function() {
//let id = jQuery(this); //getting info
//let id = (data.node.id); //not working
let id = $(this).data('id'); //not working
boxes_id.push(id);
});
return boxes_id;
}
});
我可以简单地得到这个(但它 returns 不是我需要的我可以验证我在 this
中有正确的元素):
let id = $(this).attr('id');
但是我无法得到这个(它 returns null)
let id = $(this).data('id');
我试图只提醒我所知道的关于该元素的一切:
let id = jQuery(this)
我可以看到有很多数据属性,但我无法获得其中任何一个...请问有什么想法吗?
所以我终于找到了解决办法!其实很简单!您必须使用 .map()
!
data
像这样:
$(".js-save-product").on('click', function(){
let boxes = check_checkboxes();
alert(JSON.stringify(boxes));
});
function check_checkboxes(){
let boxes_id = [];
let boxes = $('.js-categories-checkboxtree').jstree('get_checked', true)
$.each(boxes, function() {
let id = $(this).map(function() { return this.data.id }).get().join();
boxes_id.push(id);
});
return boxes_id;
}