最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Disable typing on selectize - Stack Overflow

programmeradmin2浏览0评论

As the title, on selectize, how can I disabled typing except Backspace key.

It will be allowed to:

  • Select item on dropdown.
  • Delete selected items.

It will NOT be allowed to:

  • Type or add any new items.

I have read the API document but I can't found the solution. Any suggestions.

Here mine:

var $select = $('#tags').selectize({
        maxItems: 5,
        persist: false,
        createOnBlur: true,
        create: true,
    });

UPDATE:

I found the solution by my own

$select[0].selectize.$control_input.on('keydown', function(e) {
        var key = e.charCode || e.keyCode;
        if(key == 8 )
            return true;
        else
            e.preventDefault();
    });

As the title, on selectize, how can I disabled typing except Backspace key.

It will be allowed to:

  • Select item on dropdown.
  • Delete selected items.

It will NOT be allowed to:

  • Type or add any new items.

I have read the API document but I can't found the solution. Any suggestions.

Here mine:

var $select = $('#tags').selectize({
        maxItems: 5,
        persist: false,
        createOnBlur: true,
        create: true,
    });

UPDATE:

I found the solution by my own

$select[0].selectize.$control_input.on('keydown', function(e) {
        var key = e.charCode || e.keyCode;
        if(key == 8 )
            return true;
        else
            e.preventDefault();
    });
Share Improve this question edited Oct 5, 2016 at 6:30 TommyDo asked Oct 5, 2016 at 5:02 TommyDoTommyDo 6738 silver badges25 bronze badges 1
  • The solutions listed above didn't work But this is work! stackoverflow./a/30087408/20175904 – Evgenii St. Commented Oct 7, 2022 at 9:37
Add a ment  | 

1 Answer 1

Reset to default 5

While the way you did it works, the proper way to prevent item addition is to use create: false:

var $select = $('#tags').selectize({
    maxItems: 5,
    persist: false,
    create: false
});
发布评论

评论列表(0)

  1. 暂无评论