2017-09-07 6 views
1

私は選択フィールド(複数)を持っており、カスタムタグも追加する必要があります。 しかし、カスタムデータを入力してEnterをクリックしても機能しません。 Ajaxリクエストが終了すると、私のカスタムデータがドロップダウンで表示され、その後はドロップダウンから選択することができます。入力ボタンをクリックした後にタグを追加することは可能ですか?select2 - 入力ボタンでタグを追加する

<select id="autocomplete" multiple="multiple" name="to[]"></select> 

$("#autocomplete").select2({ 
     ajax: { 
      url: url, 
      dataType: 'json', 
      cache: false 
     }, 
     tags: true,   
     minimumInputLength: 1 
    }); 

答えて

1

は(デフォルトはfalse)trueにselectOnCloseを設定しようとしたことがありますか?

selectOnClose ブールは、ドロップダウンが閉じられている自動選択を実装します。

詳細については、https://select2.org/configuration/options-apiを参照してください。最後の重要なこと:jsfiddleは、他の人があなたの問題を抱いて遊ぶのに非常に役立ちます。

更新:私はResults.highlightFirstItem機能つまずいた私の場合は - そうtrueにselectOnCloseを設定する必要はありませんでした:

Results.prototype.highlightFirstItem = function() { 
    var $options = this.$results 
     .find('.select2-results__option[aria-selected]'); 

    // Highlight the first option 
    $options.first().trigger('mouseenter'); 

    this.ensureHighlightVisible(); 
}; 

このことに注意してください:

Results.prototype.highlightFirstItem = function() { 
    var $options = this.$results 
     .find('.select2-results__option[aria-selected]'); 

    var $selected = $options.filter('[aria-selected=true]'); 

    // Check if there are any selected options 
    if ($selected.length > 0) { 
     // If there are selected options, highlight the first 
     $selected.first().trigger('mouseenter'); 
    } else { 
     // If there are no selected options, highlight the first option 
     // in the dropdown 
     $options.first().trigger('mouseenter'); 
    } 

    this.ensureHighlightVisible(); 
}; 

に変更します迅速な解決策であり、適切なリクエスト(githubの別名新しい問題)を作成する必要があります(それがあなたの問題であれば)。

関連する問題