2015-10-30 97 views
1

結果我用選擇二來從數據庫中獲取下拉結果:選擇二,選擇一個文本,如果沒有可用的

$("#loadingdata").select2({ 
    placeholder: "3 characters to go", 
    allowClear: true, 
    minimumInputLength: 3, 
    ajax: { 
     url: "ajax_cats.php", 
     dataType: 'json', 
     quietMillis: 100, 
     data: function (term, page) { // page is the one-based page number tracked by Select2 
     return { 
     q: term //search term   
     }; 
     }, 
     results: function (data) { 
     //var more = (page * 10) < data.total; // whether or not there are more results available 

     // notice we return the value of more so Select2 knows if more results can be loaded 
     return {results: data}; 
     }      
    }, 

    // user can select value 
    createSearchChoice:function(term, data) { 
     if ($(data).filter(function() { 
      return this.text.localeCompare(term)===0; }).length===0) { 
       return {id:term, text:term}; 
      } 
     }, 

    dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller   
}); 

用戶可以選擇類型值,但只有當更多的結果在數據庫中存在。

當數據庫中沒有結果可用時,是否有方法單擊以在搜索框中添加鍵入的值?

+0

當是'createSearchChoice'工作不適合你?你可以創建一個演示你所看到的行爲的jsFiddle嗎? – Dethariel

回答

1

嘗試設置標籤:真正的初始化選擇框,將暫時類型值添加到列表中

相關問題