【问题标题】:Chosen jquery - List is not loading in modal form选择 jquery - 列表未以模式形式加载
【发布时间】:2015-09-29 09:33:46
【问题描述】:

在 rails 4.2.2 中,我使用 chosen jquery 插件作为 modal 表单内的下拉(多个)字段。现在表单和字段正在加载,没有错误,但集合(列表)没有加载,如果我尝试搜索,它会显示“没有结果...”。

在控制器中,

@users = User.all.collect(&:name)

在视图中,

<%= f.input :users, :as =>:select, :label=>false, :collection =>[], :input_html=>{:multiple=>true, :class=>"form-control"} %>

在 view.js.erb 中

var content = "<%= escape_javascript(render(:partial=>"form")) %>";
var container = $('#main-lightbox-container');
container.find('.modal-content').html(content);
container.modal({});

initializeMultipleChoose("#event_users", JSON.parse('<%= raw(@users) %>'), {width: "95%", max_selected_options: 5, allow_single_deselect: true});
$("#event_users").trigger("chosen:updated");

在自定义 .js 文件中,

function initializeMultipleChoose(id, selectedOptions, parameters){
  selectedOptionsArray = $.isArray(selectedOptions) ? selectedOptions : [selectedOptions]
  object = $(id);
  $.each(object.find("option"), function(key, content){
    option = object.find(content);
    if(selectedOptionsArray.indexOf(option.val())> -1)
        option.attr('selected',true);
  })
  object.chosen(parameters);
}

加载列表需要做什么?请帮帮我。

【问题讨论】:

    标签: javascript jquery ruby-on-rails jquery-chosen


    【解决方案1】:

    也许原因是您试图在 HTML DOM 可用之前初始化所选插件?你能查一下吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-14
      • 1970-01-01
      • 2021-08-19
      • 2020-07-11
      • 1970-01-01
      • 1970-01-01
      • 2022-07-27
      相关资源
      最近更新 更多