Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.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 选择2占位符不在rails应用程序中使用隐藏的\u字段_Ruby On Rails_Jquery Ui_Jquery Select2_Select2 Rails - Fatal编程技术网

Ruby on rails 选择2占位符不在rails应用程序中使用隐藏的\u字段

Ruby on rails 选择2占位符不在rails应用程序中使用隐藏的\u字段,ruby-on-rails,jquery-ui,jquery-select2,select2-rails,Ruby On Rails,Jquery Ui,Jquery Select2,Select2 Rails,哈默: JS: Select2正在执行我现在需要的所有操作,但占位符未显示在我的视图中。任何帮助,非常感谢 我已经为此编写了一个小的coffee脚本,它可以完成所有正常的自动完成操作以及预填充的数据 $("#select2").select2({ createSearchChoice: function (term, data) { if ($(data).filter(function () { return this.text.localeCom

哈默:

JS:


Select2正在执行我现在需要的所有操作,但占位符未显示在我的视图中。任何帮助,非常感谢

我已经为此编写了一个小的coffee脚本,它可以完成所有正常的自动完成操作以及预填充的数据

 $("#select2").select2({
    createSearchChoice: function (term, data) {
      if ($(data).filter(function () {
            return this.text.localeCompare(term) === this.text;
          }).length === 0) {
        return {
          id: term,
          text: term
        };
      }
      console.log('SELECT2', $("#select2").data.text);
    },
    placeholder: "Enter custom gender or select one below",
    multiple: false,
    data: [{
      id: 'male',
      text: 'male'
    }, {
      id: 'female',
      text: 'female'
    }, {
      id: 'custom',
      text: 'custom'
    }]
});
我的隐藏场是这样的

$(document).ready ->
  $('.select2').each (i, e) =>
    select = $(e)
    options =
      placeholder: select.data('placeholder')
      minimumInputLength: 1

    if select.hasClass('ajax') # only add ajax functionality if this class is present
      options.ajax =
        url: select.data('source')
        quietMillis: 100
        dataType: 'json'
        data: (term) ->
          q: term
        results: (data) ->
          results: data.resources
          more: false

      options.dropdownCssClass = "bigdrop"
      options.initSelection = (element, callback) ->
        callback({ text: element.attr('data-value') })
    select.select2(options)

试试这个,它会有用的。

这是上面的一个好答案。我用更少的代码解决了这一问题,方法是预先设置一个空白的初始选项,由占位符填充:

hidden_field_tag(:search, '', id: 'res_select', class: 'select2 ajax form-control select-overide', style: 'width: 100%;', data: { source: "/products/autocomplete", placeholder: 'Search for a name' }, :value=> params["search"], "data-value" => params['search'])
$('.compare search').prepend(''.val)(函数(){return$('[selected]',this.val();});
hidden_field_tag(:search, '', id: 'res_select', class: 'select2 ajax form-control select-overide', style: 'width: 100%;', data: { source: "/products/autocomplete", placeholder: 'Search for a name' }, :value=> params["search"], "data-value" => params['search'])
 $('.compare-search').prepend('<option/>').val(function(){return $('[selected]',this).val() ;});