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

时间:2016-12-21 19:31:53

标签: javascript jquery selectize.js

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

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

无论如何要实现这个目标吗?

1 个答案:

答案 0 :(得分:2)

这是我试过的似乎正在起作用的东西 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;
    }
});
});