Rails - Combobox Collection依赖于另一个Combobox

时间:2015-01-12 20:17:27

标签: ruby-on-rails combobox

我正在寻找一种方法来改变Combobox Collection(combo_two)取决于选择另一个组合框(combo_one)。请按照Rails表格进行操作:

<%= form_for(@requisite) do |f| %>
  <div class="field">
    <%= f.label :this_type %><br>
    <%= f.text_field :this_type, :value => params["type"] %>
  </div>
  <div class="field">
    <%= f.label :this_id %><br>
    <%= f.number_field :this_id, :value => params["id"] %>
  </div>
  <div id="combo_one" class="field">
    <%= f.label :require_type %><br>
    <%= f.select :require_type, ([["Element","Element"],["Research","Research"]]) %>
  </div>
  <div id="combo_two" class="field">
    <%= f.label :require_id %><br>
        <%= f.collection_select(:require_id, @elements, :id, :name, :prompt => true) %>
  </div>
  <div class="field">
    <%= f.label :qty %><br>
    <%= f.number_field :qty %>
  </div>
  <div class="field">
    <%= f.label :lvl %><br>
    <%= f.number_field :lvl %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

我希望我们用javascript(jquery)解决它,但我不知道javascript(jquery)

1 个答案:

答案 0 :(得分:0)

使用jquery你有可能在组合框中使用html onchange事件获取第一个组合框的值

<%= f.select :require_type, ([["Element","Element"],["Research","Research"]]),:onchange => GetValue(this) %>

<select class="combo_box" onchange="GetValue(this);"></select> 

function GetValue(this){
    var value = this.value;
    //Another code for second combobox
}