2014-04-08 54 views
1

我想提交一個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 
} 

但我的窗體只提交本:是我的Rails產生

{ 
    "utf8" = > "✓", 
     "authenticity_token" = > "9gf3upm65ugEhsNvdcaykjdlg7xZbOyTiWJs79SnY3A=", 
     "timespan_id" = > { 
     "name" = > ["", 
      ""] 
    }, 
     "subgroup_id" = > "369141985" 
} 

形式。它看起來像這樣:

<%= 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="" /> 

我想,有什麼不對的這兩個。但我沒有得到什麼。 你有什麼想法嗎?


@МалъСкрылевъ:它必須這樣。 的形式應該是這樣的: enter image description here

+0

你有兩個選擇標籤定義:'<%=選擇(:timespan_id,:名稱...' –

+4

如何做一個表單中有一個值 – epascarello

+0

@epascarello:我應該設置什麼值而不是值? – user3383458

回答

0

我發現我的問題的解決方案: 我不得不把選擇框的形式,去掉了這個參數之外:

"timespan_id" = > { 
        "name" = > ["", 
           ""] 

然後我在表單中添加一個名爲'string'的hidden_field_tag。 後來終於,我改變了我的保存功能:

var save=function() 
{ 
var left = document.getElementById("left"); 
var result; 
result=""; 
for(i=0;i<left.length;i++) 
    {  
    result = result+","+left.options[i].value; 
    }  
var string=document.getElementById("string"); 
alert(result); 
string.value=result; 
document.exchange.submit(); 
} 
相關問題