如何限制选择标签中的最小字符

Fai*_*zan 6 jquery selectize.js

我想限制Selectize标签输入的最少3个字符.可能吗?选择中是否有任何事件?

Igo*_*ino 6

我有同样的问题.正如Rory所提到的那样,通过插件.

它其实很简单.

标签最小字长过滤的官方示例,您可以在这里找到

$('#select-words-length').selectize({
    create: true,
    createFilter: function(input) { return input.length >= MIN_LENGTH; }
});
Run Code Online (Sandbox Code Playgroud)

您可以做的另一件事是过滤搜索本身

//restricts the matches to fulfill MIN_SEARCH_LENGTH via the 'score' callback
//see https://github.com/brianreavis/selectize.js/blob/master/docs/usage.md#callbacks
score: function scoreFilter(search) {
    var ignore = search && search.length < MIN_SEARCH_LENGTH;
    var score = this.getScoreFunction(search);
    //the "search" argument is a Search object (see https://github.com/brianreavis/selectize.js/blob/master/docs/usage.md#search).
    return function onScore(item) {
        if (ignore) {
            //If 0, the option is declared not a match.
            return 0;
        } else {
            var result = score(item);
            return result;
        }
    };
},
Run Code Online (Sandbox Code Playgroud)

希望有帮助:)


小智 -7

  1. 下载 selectize.js 插件

  2. 包括 jquery 和

使用此代码,它会起作用。

$('#your-id').selectize({ maxItems: 3 });