2017-02-19 24 views
0

私は以下のコーヒースクリプトを持っています。リモートAjax select2を実行すると、これはすべてのクラスselect2-autocompleteを設定します。問題は、このスクリプトを追加したばかりの項目以下 coffeescriptのjqueryセレクタからそれぞれを削除します

$('.select2-autocomplete').each (i, e) -> 
    select = $(e) 
    options = 
     multiple: false 
     width: "98%" 
     placeholder: "Type Hotel name" 
     minimumInputLength: 3 
    options.ajax = 
     url: select.data('source') 
     dataType: 'json' 
     type: "GET" 
     quietMillis: 150 
     # input untuk program 
     data: (term) -> 
     q: term 
     results: (data) -> 
     results: $.map(data, (item) -> 
      text: item.name 
      id: item.id 
     ) 
    options.dropdownCssClass = 'bigdrop' 
    select.select2 options 

は私のHTMLコード、ノートはどのように私は反復各せずに以下のスクリプトを変更し、:私はSELECT2のv.3.5.3のAJAXを使用していますので、私は隠しフィールド(レール)

 <%= f.hidden_field :hotel_id, data: { source: search_name_hotels_path }, class: "select2-autocomplete", :value => "#{f.object.hotel_id unless f.object.new_record? || f.object.hotel_id.nil? }" %> 
を使用しています
+0

以下のようにあなたがありがとうコード –

答えて

0

呼び出されたときに要素に対して実行される小さなjQueryプラグインを作成します。

$.fn.enableAutocomplete =() -> 
    select = this 
    options = 
     multiple: false 
     width: "98%" 
     placeholder: "Type Hotel name" 
     minimumInputLength: 3 
    options.ajax = 
     url: select.data('source') 
     dataType: 'json' 
     type: "GET" 
     quietMillis: 150 
     # input untuk program 
     data: (term) -> 
     q: term 
     results: (data) -> 
     results: $.map(data, (item) -> 
      text: item.name 
      id: item.id 
     ) 
    options.dropdownCssClass = 'bigdrop' 
    select.select2 options 

これを有効にする要素でこれを呼び出します。

$('#selectElement').enableAutocomplete(); 
+0

をhtmlの共有してください、あなたのコメントは、上記直接に接続されていないが、私は私の問題を解決する助けました。 – widjajayd

関連する問題