如果不满足某些条件,则防止打开下拉菜单

Prevent dropdown from being opened if certain condition is not met

当用户在文本框中输入查询时,我正在使用 selectize.js 提供一些建议。但是,如果不满足某些条件,我想阻止打开下拉菜单。我知道 onDropdownOpen 回调,但似乎无法从那里停止事件。以下解决方法也不起作用。

onDropdownOpen: function($dropdown) {
    // [Test some variables here]
    $dropdown.collapse();
}

有没有办法实现这个?

这是我尝试过的方法,似乎有效 https://plnkr.co/edit/DifihIuXoGkMxfdEht9L?p=preview

$(document).ready(function(){
  $('#input-tags').selectize({
    delimiter: ',',
    persist: false,
    create: function(input) {
        return {
            value: input,
            text: input
        }
    },
    onDropdownOpen: function(){
      // you can add your logic here to conditionally close the drop down
      this.close(); 
      // I had to set it to false because when it is true, this will run into infinite loop since while the input is in focus, it will trigger to open the drop down.
      this.settings.openOnFocus = false;
    }
});
});