我试图data-id
从用 JsTree(复选框表单)生成的树形表单中获取,但它不起作用。我正在使用$.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;
}
});
我可以简单地得到这个(但它返回的不是我需要的,我可以验证我在 中有正确的元素this
):
let id = $(this).attr('id');
但我无法得到这个(它返回空值)
let id = $(this).data('id');
我试图提醒我对元素的所有了解:
let id = jQuery(this)
我可以看到有很多数据属性,但我无法获得其中任何一个......请问有什么想法吗?
所以我终于找到了解决方案!其实很简单!您必须data
使用.map()
!
像这样:
$(".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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句