Jquery 选择2自定义匹配器
我正在尝试使用select2库的自定义匹配器。具体来说,我想返回Jquery 选择2自定义匹配器,jquery,jquery-select2,Jquery,Jquery Select2,我正在尝试使用select2库的自定义匹配器。具体来说,我想返回other作为notfound选项,并且只从字符串的开头进行匹配。我发现以下问题分别回答了这些部分: 及 然而,当我结合这两种技术时,它不再正确匹配。我的解决方案如下所示: $("#myForm").select2({ minimumInputLength: 2, width: width, matcher: function(term, text) { // THIS IS WHERE
other
作为notfound选项,并且只从字符串的开头进行匹配。我发现以下问题分别回答了这些部分:
及
然而,当我结合这两种技术时,它不再正确匹配。我的解决方案如下所示:
$("#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;
}
});
我做错了什么,使这个matcher函数的正确方法是什么?我使用正则表达式并通过将条件
|
分为两个步骤来实现这一点。最终代码是:
$("#myForm").select2({
minimumInputLength: 2,
width: width,
matcher: function(term, text) {
var terms = term.split(" ");
for (var i=0; i < terms.length; i++){
var tester = new RegExp("\\b" + terms[i], 'i');
if (tester.test(text) == false){
return (text === 'Other')
}
}
return true;
},
sortResults: function(results) {
if (results.length > 1) {
results.pop();
}
return results;
},
});
$(“#myForm”)。选择2({
最小输入长度:2,
宽度:宽度,
匹配器:函数(术语、文本){
var术语=术语拆分(“”);
对于(var i=0;i1){
结果:pop();
}
返回结果;
},
});