jQuery select2插件如何只匹配单词的开头 [英] jquery select2 plugin how to match only beginning of word

查看:130
本文介绍了jQuery select2插件如何只匹配单词的开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网站上使用的是select2,我希望自动填充功能仅匹配单词的开头.例如,如果我键入"CA",我希望出现CAmeroun,而不是"vatiCAn".

I am using select2 in my website and i want the autocomplete to match only the beginning of the word. For example, if I type "CA" I want CAmeroun to appear and not "vatiCAn".

推荐答案

我想出了如何通过搜索文档来解决此问题的方法(此处

I figured out how to resolve this by searching in the documentation (here https://github.com/select2/select2/issues/428).

在select2库中,替换为select2.js:

In select2 library, replace in select2.js :

matcher: function(term, text) {
        return stripDiacritics(''+text).toUpperCase().indexOf(stripDiacritics(''+term).toUpperCase()) >= 0;
    },

作者:

matcher: function(term, text) {
       if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
        return true;
      }
    },

还有tadaaa.有用.我希望精通JS的人(99%的JS开发人员)能够给出更好的答案或创建一个好的补丁.

And tadaaa. It works. I hope someone who is better in JS (99% of JS developers) could give a better answer or create a good patch.

别忘了缩小您的JS;)!

Don't forget to minify your JS ;) !

这篇关于jQuery select2插件如何只匹配单词的开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆