2015-09-19 126 views
0

更新选择选项时,我有一个形成两个选择:丢失后数据用jQuery

  • input_licensee_list:充满可能的选项(使用Ajax调用)
  • output_licensee_list:获得来自用户的选择。

当我发表表格时,我收到了output_licensee_list的列表。为此,我添加了两个按钮:添加(将所选选项从输入移动到输出)和添加全部(将所有选项从输入移动到输出)。

// Add all button: remove all entries from input list and add them to output list 
add_all_button.on('click', function (e) { 
    var options = input_licensee_list.find("option").remove(); 
    options.append(' (' + group_selection.find("option:selected").text() + ')'); 
    options.appendTo(output_licensee_list); 
    output_licensee_list.trigger('change'); 
    input_licensee_list.trigger('change'); 
}); 

// Add button: remove selected entries from input list and add them to output list 
add_button.on('click', function (e) { 
    var options = input_licensee_list.find("option:selected").remove(); 
    options.append(' (' + group_selection.find("option:selected").text() + ')'); 
    options.appendTo(output_licensee_list); 
    output_licensee_list.trigger('change'); 
    input_licensee_list.trigger('change'); 
}); 

我准备了的jsfiddle显示它:

https://jsfiddle.net/zaun8qgp/4/

jQuery代码本身似乎做工精细,并且形式正确张贴,如果我使用“添加”按钮(即使如果我选择所有选项)。

但是出于某种原因,如果我使用“全部添加”按钮:

  • 输入output_licensee_list没有出现在POST可言,所以我没有得到任何信息;
  • 如果我使用“添加”,然后“添加全部”,我在帖子中得到的信息,如果我最初添加的列表(在全部添加之前)。

如果任何人有线索为什么它不能正常工作,或者我可以如何调试更多,我将不胜感激!

回答

1

你的问题是,提交一个包含<select>只提交选定的选项。您在“全部添加”中添加的选项未被选中。

解决这个问题的最简单的方法是添加:

options.prop('selected', 'true'); 

你的形式,以确保所有选项在提交之前选择提交事件处理程序。

+0

太简单了!谢谢,我可以找几天... – Dric512