Javascript 基于Ruby-on-Rails复选框的动态数组

Javascript 基于Ruby-on-Rails复选框的动态数组,javascript,ruby-on-rails,ruby,ruby-on-rails-3,Javascript,Ruby On Rails,Ruby,Ruby On Rails 3,我有两个复选框列表。一个是存储列表,另一个是属于这些存储的分类列表。现在,无论选择了哪个存储,都会显示所有分类法。如何显示仅属于所选存储的分类 html.erb 在我看来: 但是我不知道这个方法的名字 我希望这些存储复选框可以通过切换功能控制显示哪些分类法。您需要两件事: 在两个数据集之间映射的方法。例如,哪些分类属于哪些存储的地图 基于第一组复选框交换第二组复选框中的值的方法。jQuery是一个很好的候选者,但任何javascript甚至页面重新加载都可以 这里有一个类似的问题,还有一些jsf

我有两个复选框列表。一个是存储列表,另一个是属于这些存储的分类列表。现在,无论选择了哪个存储,都会显示所有分类法。如何显示仅属于所选存储的分类

html.erb

在我看来:

但是我不知道这个方法的名字

我希望这些存储复选框可以通过切换功能控制显示哪些分类法。

您需要两件事:

在两个数据集之间映射的方法。例如,哪些分类属于哪些存储的地图

基于第一组复选框交换第二组复选框中的值的方法。jQuery是一个很好的候选者,但任何javascript甚至页面重新加载都可以

这里有一个类似的问题,还有一些jsfiddle应该可以帮助您。它们使用下拉列表而不是复选框,但概念完全相同:

<h3>Stores Offered In</h3>
  <ul class="multi-column-checkbox">
    <% for store in Store.all %>
        <li><%= check_box_tag "idea[store_ids][]", store.id, 
@idea.stores.include?(store) %> <%= store.name %></li>
    <% end %>
  </ul>
  <br />


  <h3>Taxonomies Offered In</h3>
      <% for store in Store.all %>
     <% if store.has_taxonomies? %>
          <ul class="multi-column-checkbox-taxonomies" >
            <h4><%= store.name %></h4>
                <% for taxonomy in store.taxonomies 
%>              
                <li><%= check_box_tag "idea[taxonomy_ids][]", 
taxonomy.id, @idea.taxonomies.include?(taxonomy) %> <%= taxonomy.name %></li>
            <% end %>
          </ul>
     <% end %>
  <% end %>
class Taxonomy < ActiveRecord::Base
  validates_presence_of :name, :store_id
  belongs_to :store
  has_and_belongs_to_many :ideas, :join_table => "taxonomies_ideas"

  validates_uniqueness_of :name, :scope => :store_id
end
class Store < ActiveRecord::Base
  validates_uniqueness_of :code
  has_and_belongs_to_many :ideas, :join_table => "stores_ideas"
  has_many :taxonomies

  def has_taxonomies?
    taxonomies.count > 0
  end
end
def show_hide(show)
  show ? 'block' : 'none'
end
 <h3>Taxonomies Offered In</h3>
      <% for store in Store.all %>
     <% if store.has_taxonomies? %>
            <ul class="multi-column-checkbox-taxonomies" style="display: <%= 
      show_hide(@application.______?)%>;" >
            <h4><%= store.name %></h4>
                <% for taxonomy in store.taxonomies 
%>              
                <li><%= check_box_tag "idea[taxonomy_ids][]", 
taxonomy.id, @idea.taxonomies.include?(taxonomy) %> <%= taxonomy.name %></li>
            <% end %>
          </ul>
     <% end %>