【发布时间】:2014-04-08 12:53:33
【问题描述】:
我正在尝试提交一个 javscript 生成的值。 脚本是:
var save = function () {
var left = document.getElementById("left"); //Get a select box
var result = document.exchange; //get the form
result.value = ""; //set it's value to ""
for (i = 0; i < right.length; i++) {
result.value = result.value + "," + left.options[i].value; //set it's value to the values of the select box, divided by commas.
}
result.submit(); //submit the form
}
但我的表单只提交这个:
{
"utf8" = > "✓",
"authenticity_token" = > "9gf3upm65ugEhsNvdcaykjdlg7xZbOyTiWJs79SnY3A=",
"timespan_id" = > {
"name" = > ["",
""]
},
"subgroup_id" = > "369141985"
}
表单是由我的 Rails 生成的。它看起来像这样:
<%= form_tag(subgroup_change_timespans_path(@subgroup), {
id: 'exchange',
name: 'exchange'
}) do %> <% timespans_array = @subgroup.timespans.all.map { | timespan | [timespan.name, timespan.id]
} %> <%= select(: timespan_id, : name, options_for_select(timespans_array), {}, {: multiple = > true,
: style = > "width: 300px; background-color: #9FE",
: width = > "300",
: size = > 20,
: id = > "left"
}) %> <%= link_to "<<", {
anchor: "",
remote: true
}, {
id: "toleft",
w_command: "add_timespan",
w_auth: form_authenticity_token,
w_controller: "subgroups",
w_id: @subgroup.id.to_s
} %> <%= link_to ">>", {
anchor: "",
remote: true
}, {
id: "toright",
w_command: "rem_timespan",
w_auth: form_authenticity_token,
w_controller: "subgroups",
w_id: @subgroup.id.to_s
} %> <% timespans_array = Timespan.all.map { | timespan | [timespan.name, timespan.id]
} %> <%= select(: timespan_id, : name, options_for_select(timespans_array), {}, {: multiple = > true,
: style = > "width: 300px; background-color: #F99",
: width = > "300",
: size = > 20,
: id = > "right"
}) %> <%= link_to "save", {
anchor: "",
remote: true
}, {
id: "save"
} %> <% end %>
但正如我告诉你的那样: 它只是提交两个隐藏字段的名称:
<input name="timespan_id[name][]" type="hidden" value="" />
<input name="timespan_id[name][]" type="hidden" value="" />
我认为,这两个有问题。但我不明白什么。 你有什么想法吗?
@Малъ Скрылевъ:必须这样。 表格应如下所示:
【问题讨论】:
-
您定义了两个选择标签:
<%= select(:timespan_id, :name ... -
表单如何有值?
-
@epascarello:我应该设置什么而不是值?
-
我不知道你想做什么,因为
<form>没有值,所以没有什么可设置的。 -
你应该设置一个文本框/文本区域/输入并将其提交到服务器...
标签: javascript ruby-on-rails ruby forms