我有这两个功能:
$("#form_pdetail").on("click", "#register_button", function() {
var detail_add = $("#form_pdetail").serialize();
var request = $.ajax({
type: 'POST',
url: "{{ path('product_detail_create') }}",
data: detail_add,
success: function() {
request.abort();
loadCenterLayout("{{ path('product_detail_list') }}");
}
});
});
$("#tree").dynatree({
checkbox: true,
children: {% autoescape false %} {{ categories }} {% endautoescape %},
minExpandLevel: 1,
selectMode: 3,
onSelect: function(select, node) {
// Get a list of all selected nodes, and convert to a key array:
var selKeys = $.map(node.tree.getSelectedNodes(), function(node){
return node.data.key;
});
console.log(selKeys.join(", "));
},
onKeydown: function(node, event) {
if( event.which == 32 ) {
node.toggleSelect();
return false;
}
}
});
在序列化detail_add
并提交表单之前,我需要将第二个函数中的选定选项传递给第一个函数吗?
答案 0 :(得分:1)
您可以通过获取对树的引用来获取它,而不是在dynatree对象的onSelect事件中获取所选节点。
来自dynaTree文档:
var tree = $("#tree").dynatree("getTree");
之后你应该能够做到:
var selKeys = $.map(tree.getSelectedNodes(), ..................
您可以在表单的注册按钮的点击功能中完成所有这些操作。
也许是这样的:
$("#form_pdetail").on("click", "#register_button", function() {
var detail_add = $("#form_pdetail").serialize();
var tree = $("#tree").dynatree("getTree");
var selKeys = $.map(tree.getSelectedNodes(), function(node) {
return node.data.key;
}
detail_add.selectedItems = selKeys.join(", ");
var request = $.ajax({
type: 'POST',
url: "{{ path('product_detail_create') }}",
data: detail_add,
success: function() {
request.abort();
loadCenterLayout("{{ path('product_detail_list') }}");
}
});
});