【发布时间】:2014-07-12 01:26:20
【问题描述】:
我正在尝试将自定义匹配器与 select2 库一起使用。具体来说,我想返回 other 作为未找到的选项,以及仅从字符串的开头匹配。我发现以下 SO 问题分别回答了这些部分:
Select2, when no option matches, "other" should appear
和
jquery select2 plugin how to get only the result 'myString%'
但是,当我结合这两种技术时,它不再正确匹配。我的解决方案如下:
$("#myForm").select2({
minimumInputLength: 2,
width: width,
matcher: function(term, text) {
// THIS IS WHERE I COMBINE THE METHODS
return text === 'Other' || text.toUpperCase().indexOf(term.toUpperCase())==0;
},
sortResults: function(results) {
if (results.length > 1) results.pop();
return results;
}
});
我做错了什么,使这个匹配器功能的正确方法是什么?
【问题讨论】:
标签: jquery jquery-select2