2015-11-02 53 views
1

我加載幾個Selectize選擇輸入在一個頁面中,像這樣:選項添加到多個Selectize輸入接口類型

var selectizeInput = []; 
$('.select-photo-id').each(function (i) { 
    var selectedValue = $(this).val(); 
    selectizeInput[i + 1] = $(this).selectize({ 
     'maxOptions': 100, 
     'items': [selectedValue], 
     'onType': function (input) { 
      $.post("admin/ajax/search_photos_for_postcards", 
       {input: input}, 
       function (data) { 
        $(this).addOption(data); 
        $(this).refreshOptions(); 
       }, 'json'); 
     } 
    }); 

}); 

事件onType,使函數調用返回的,我想新的選項列表立即在Selectize輸入中提供。有沒有辦法從那裏調用Selectize實例?從代碼中可以看到,我嘗試使用$(this)訪問它,但它失敗。我也試過$(this).selectize,但它是一樣的。哪種方法是正確的?

回答

0

我設法解決它:

'onType': function (input) { 
     var $this = $(this); 
     $.post("admin/ajax/search_photos_for_postcards", 
      {input: input}, 
      function (data) { 
       $this[0].addOption(data); 
       $this[0].refreshOptions(); 
      }, 'json'); 
    } 
0

你可能想使用由Selectize.js API提供的load事件是the demos看到。滾動,直到找到「Remote Source - Github」,然後點擊它下面的「顯示代碼」。