更改选择的 jquery 插件中的搜索行为
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9274362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing search behavior in jquery plugin Chosen
提问by David W.
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.
我正在使用 jQuery 的 Chosen 插件,并希望搜索行为有所改变(单选)。搜索结果只匹配搜索字符串中单词开头的匹配项。我想将其扩展到斜线和括号后的单词。
For example: search string: "second" does not match items "first/second" or "first (second)".
例如:搜索字符串:“ second”不匹配项目“ first/second”或“ first (second)”。
I doubt this is changeble by simply adding options to constructor, but I am willing to change/hardcode source script.
我怀疑这是否可以通过简单地向构造函数添加选项来改变,但我愿意更改/硬编码源脚本。
回答by Didier Ghys
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.
如果您愿意更改插件源本身,这是一种方法。
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:
在插件中进行搜索的方法是Chosen.prototype.winnow_results
. 它使用一个正则表达式来匹配“以”搜索词开头的文本:
// "^": 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');
Change it to:
将其更改为:
regex = new RegExp(searchText.replace(/[-[\]{}()*+?.,\^$|#\s]/g, "\$&"), 'i');
回答by Lasse Skindstad Ebert
The search behavior can be set with the option search_contains
可以使用选项设置搜索行为 search_contains
This is by default false
这是默认的 false
Set it to true
, and chosen will also find matches inside instead of only the beginning:
将其设置为true
, 而 selected 也会在内部找到匹配项,而不仅仅是开头:
$('#my_dropdown').chosen({ search_contains: true });
回答by Dirk Nguyen
As in Chosen 1.0, just add option {search_contains: true}
在 Chosen 1.0 中,只需添加选项 {search_contains: true}
$('.selector').chosen({search_contains: true});
Have fun.
玩得开心。
回答by Lovepreet Singh
There is option search_contains
available to search sub-string in options and can be used as:
有选项search_contains
可用于搜索选项中的子字符串,可用作:
$(".chosen-select").chosen({
search_contains: true
});
回答by Dave Driesmans
in chosen 1.0 i did on line 301&302
在选择的 1.0 中,我在第 301 和 302 行做了
escapedSearchText = searchText.replace(/[-[\]{}()*+?.,\^$|#\s]/g, "\$&");
regexAnchor = "";