更改 jquery 插件中的搜索行为选择 [英] Changing search behavior in jquery plugin Chosen

查看:13
本文介绍了更改 jquery 插件中的搜索行为选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jQuery 的 Chosen 插件,并希望搜索行为有所改变(单选).搜索仅会导致匹配字符串中单词开头的命中.我想将其扩展为在斜线和括号之后也命中单词.

I'm using the Chosen plugin for jQuery and would like the search behavior to change a bit (single select). Searching only results in hits where the beginning of a word in the seach string matches. I would like to extend this to also hit words after slash and brackets.

例如:搜索字符串:second"与项目first/second"或first(second)"不匹配.

For example: search string: "second" does not match items "first/second" or "first (second)".

我怀疑这是否可以通过简单地向构造函数添加选项来改变,但我愿意更改/硬编码源脚本.

I doubt this is changeble by simply adding options to constructor, but I am willing to change/hardcode source script.

选择:https://github.com/harvesthq/chosen

推荐答案

正如在最近的一些答案中提到的,该插件现在实现了一个更改搜索行为的选项:

As mentionned in some more recent answers, the plugin now implemements an option to change the search behavior:

search_contains: true

选项文档

插件不提供更改搜索方法行为的选项.

The plugin does not provide an option to change the search method behavior.

如果您愿意更改插件源本身,这里有一个方法.

If you're willing to change the plugin source itself, here's a way to do it.

在插件中进行搜索的方法是Chosen.prototype.winnow_results.它使用正则表达式匹配以搜索词开头"的文本:

The method that makes the search in the plugin is Chosen.prototype.winnow_results. It uses a regular expression that matches text that "starts with" the search term:

// "^": means "starts with"
// "searchText" is the text in the search input (it is just cleaned up don't be scared)
regex = new RegExp('^' + searchText.replace(/[-[]{}()*+?.,\^$|#s]/g, "\$&"), 'i');

改成:

regex = new RegExp(searchText.replace(/[-[]{}()*+?.,\^$|#s]/g, "\$&"), 'i');

演示

这篇关于更改 jquery 插件中的搜索行为选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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