1

我有模型DogCategory它们的表格在同一页上。我正在使用Chosen Plugin,当我添加一个类别时,它不会更新狗表单的类别选择。它们都是远程表单,创建方式相同,所以 new.js.erb 和 create.js.erb 基本相同:

宠物/index.html.erb

<div class="tabbable">
   <ul class="nav nav-tabs">
      <li class="active">
         <a href="#tab1" data-toggle="tab">Dog</a>
      </li>
   </ul>
   <ul class="nav nav-tabs">
      <li class="active">
         <a href="#tab2" data-toggle="tab">Category</a>
      </li>
   </ul>
   <div class="tab-content">
      <div class="tab-pane active add-dog-form" id="tab1">
         <%= render "dogs/form" %>
      </div>
   </div>
   <div class="tab-content">
      <div class="tab-pane active add-category-form" id="tab2">
         <%= render "categories/form" %>
      </div>
   </div>
</div>

狗/new.js.erb

$('.add-dog-form').html('<%= escape_javascript(render(:partial => 'dogs/form', locals: { dog: @dog })) %>');

狗/create.js.erb

$('.add-dog-form').html('<%= escape_javascript(render(:partial => 'dogs/form', locals: { dog: @dog })) %>');

狗/_form.html.erb

<%= form_for(@dog, :remote => true) do |f| %>
   <%= f.text_field :name %>
   <%= f.select :category_ids, Category.all.collect {|c| [c.name, c.id]}, {},
        { :multiple => true, :class => "category-select" } %>
   <%= f.submit %>
<% end %>

应用程序.js

jQuery( function($) {
    // Chosen Select Menu
    $('.category-select').chosen().trigger("liszt:updated");
});

还有其他人使用此插件和/或知道如何更新选择菜单吗?

4

1 回答 1

3

application.js 而不是:

$('.category-select').chosen().trigger("liszt:updated");

利用:

$('.category-select').trigger("liszt:updated");
于 2012-08-04T14:05:27.593 回答