jquery-select2之按 id 和 Select2 中的文本过滤
sky-heaven
阅读:25
2025-05-04 20:05:19
评论:0
var data = [{ id: 11-07, text: 'enhancement' }, { id: 11-04, text: 'bug' },
{ id: 11-08, text: 'duplicate' }, { id: 11-09, text: 'invalid' },
{ id: 11-10, text: 'wontfix' }];
$(".js-example-data-array").select2({
data: data
})
<select class="js-example-data-array"></select>
我想用 id 和文本过滤 select2。默认情况下,它按文本过滤。
我怎样才能按 id 过滤?
请您参考如下方法:
您必须使用“匹配器”。
$(".js-example-data-array").select2({
data: data,
matcher: function(term, text, option) {
return text.toUpperCase().indexOf(term.toUpperCase())>=0 || option.val().toUpperCase().indexOf(term.toUpperCase())>=0;
}
})
它将返回按文本或 id 过滤的结果。
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。