Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails cocoon select2 ajax部分加载-select2元素未加载_Ruby On Rails_Jquery Select2_Cocoon Gem - Fatal编程技术网

Ruby on rails cocoon select2 ajax部分加载-select2元素未加载

Ruby on rails cocoon select2 ajax部分加载-select2元素未加载,ruby-on-rails,jquery-select2,cocoon-gem,Ruby On Rails,Jquery Select2,Cocoon Gem,我有一个rails应用程序,它使用cocoon =链接到添加关联 调用部分形式 在主窗体上,我使用coffee脚本加载select2元素的所有数据 插入ajax部分时,select2元素不会出现。我需要实例化它 这是我的表格咖啡/js $(document).ready -> $(".select2").each (i, e) -> select = $(e) options = {} if select.hasClass("ajax")

我有一个rails应用程序,它使用cocoon

=链接到添加关联

调用部分形式

在主窗体上,我使用coffee脚本加载select2元素的所有数据

插入ajax部分时,select2元素不会出现。我需要实例化它

这是我的表格咖啡/js

  $(document).ready ->
   $(".select2").each (i, e) ->
    select = $(e)
    options = {}
    if select.hasClass("ajax")
      options.ajax =
        url: select.data("source")
        dataType: "json"
        data: (term, page) ->
          q: term
          page: page
          per: 10
        results: (data, page) ->
          results: data
      options.placeholder = "Select a value"
      options.allowClear= "true"
      options.dropdownAutoWidth = "true"
      options.initSelection = (element, callback) ->
        data = {id: element.val().split('||')[0], text: element.val().split('||')[1]};
        callback data
    select.select2 options
    return
如果我在绑定到后使用coocon-insert

  $('body').bind 'cocoon:after-insert', (e, inserted_item) ->
    $(".select2").each (i, e) ->
      select = $(e)
      options = {}
      if select.hasClass("ajax")
        options.ajax =
          url: select.data("source")
          dataType: "json"
          data: (term, page) ->
            q: term
            page: page
            per: 10
          results: (data, page) ->
            results: data
        options.placeholder = "Select a value"
        options.allowClear= "true"
        options.dropdownAutoWidth = "true"
        options.initSelection = (element, callback) ->
          data = {id: element.val().split('||')[0], text: element.val().split('||')[1]};
          callback data
      select.select2 options
      return
当我调用所有select2对象时,我会刷新页面上的所有元素。我没有为Select2JS编写此代码

所有现有的表单元素都是可以的,但是那些动态添加的元素会被刷新,所以它们会丢失它们拥有的值

我只想选择添加的元素并使其工作

如果我尝试

$('body').bind 'cocoon:after-insert', (e, inserted_item) ->
  $(inserted_item).find(".select2").select2
  return
它也不起作用

尝试了很多选择,但我的头发现在很薄,我需要帮助。JS是我最大的敌人,我真的觉得很痛苦

救命啊

天哪。在这上面花了几个小时,然后我就得到了……

$(插入的项目)。查找(“.select2”)。选择2
$(document).ready ->
  $('body').bind "cocoon:after-insert", (e, inserted_item) ->
    select=$(inserted_item).find(".select2.ajax")
    options = {}
    if select.hasClass("ajax")
      options.ajax =
        url: select.data("source")
        dataType: "json"
        data: (term, page) ->
          q: term
          page: page
          per: 10
        results: (data, page) ->
          results: data
      options.placeholder = "Select a value"
      options.allowClear= "true"
      options.dropdownAutoWidth = "true"
      options.initSelection = (element, callback) ->
        data = {id: element.val().split('||')[0], text: element.val().split('||')[1]};
        callback data
    select.select2 options
    return