为什么选择的插件没有更新?

时间:2012-08-04 13:57:58

标签: jquery-chosen

我有模型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>

dogs / 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 %>

的application.js

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

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

1 个答案:

答案 0 :(得分:3)

application.js 而不是:

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

使用:

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