具有多个选择框的表单在轨道中无法正常工作

时间:2016-10-17 06:45:49

标签: javascript ruby-on-rails

有人可以解释一下为什么我的表格无法正常运作:

 <%= form_for current_user, url: change_my_team_path, method: :put, remote: true, id: "my-team-form" do |f| %>

                  <div class="countries" id="england">
                      <%= f.select :team, options_for_select(select_apl_teams), {:include_blank => change_blank(current_user)}, {:onchange=>"$(this.form).submit();", class: "selectpicker", "data-live-search"=>"true"} %>
                  </div> 
                  <div class="countries" id="spain">
                      <%= f.select :team, options_for_select(select_laliga_teams), {:include_blank => change_blank(current_user)}, {:onchange=>"$(this.form).submit();", class: "selectpicker", "data-live-search"=>"true"} %>
                  </div>
                  <div class="countries" id="italy">
                      <%= f.select :team, options_for_select(select_seria_teams), {:include_blank => change_blank(current_user)}, {:onchange=>"$(this.form).submit();", class: "selectpicker", "data-live-search"=>"true"} %>
                  </div>
                  <div class="countries" id="germany">
                      <%= f.select :team, options_for_select(select_bundesliga_teams), {:include_blank => change_blank(current_user)}, {:onchange=>"$(this.form).submit();", class: "selectpicker", "data-live-search"=>"true"} %>
                  </div>

 <% end %>

此表单仅在我使用上一个选择框时才有效,前3个不起作用。当我改变订单时,我有相同的行为,最后一个选择框正在运行,前三个没有工作。

我有理由在一种形式中使用4个选择框。如果有人有任何想法,我将不胜感激。

2 个答案:

答案 0 :(得分:0)

f.select :team将生成具有相同名称和ID的选择框,因此首先它会为每个节点违反HTML唯一ID规则,因为它具有相同的名称并且rails从元素名称获取参数,它从中获取值最后一个元素。

答案 1 :(得分:0)

是的,它不起作用,因为每次添加新select时都会重新定义属性,您必须使用一个带multiple属性的选择框:

f.select :team, options_for_select(select_laliga_teams), multiple: true

在这种情况下,您可以使用推送ctrl按钮

选择多个值