select2搜索预加载列表为typed

时间:2014-07-15 22:27:09

标签: javascript jquery jquery-select2

我在选择菜单上使用Select2,当用户输入搜索字段时,Select2将搜索这些字母并显示它们,即使它们只是出现在选项的中间。例如,如果我有这样的选择:

 <select id="e1">
    <option value="apple">Apple</option>
    <option value="grape">Grape</option>
    <option value="prune">Prune</option>
</select>

我的Select2代码如下所示:

$('#e1').select2({
                    placeholder: "",
                    allowClear: true,
                    width: '600px',
                    selectOnBlur: true,
                    scrollpad: true,
                });

用户只需在搜索框中输入“p”,它就会显示所有三个选项。我想在那种情况下发生的只是显示“修剪”。有没有办法做到这一点?我环顾了谷歌和文档,要么我错过了一些非常关键的东西,要么就是不存在。有人在这件事上有任何智慧的话语吗?

1 个答案:

答案 0 :(得分:1)

您只需提供自定义matcher设置。

这是当前默认的匹配器功能:

function(term, text) { 
    return text.toUpperCase().indexOf(term.toUpperCase())>=0;
}

例如,你可以用

代替它
function(term, text) { 
    return text.substr(0, term.length).toUpperCase() == term.toUpperCase();
}

示例设置:

$("#e1").select2({
    // ...other options...
    matcher: function(term, text) { ... }
});
相关问题