在jquery插件中更改搜索行为选择

Dav*_* W. 40 jquery jquery-plugins

我正在使用josen的Chosen插件,并希望搜索行为改变一点(单选).仅搜索会导致搜索字符串中单词的开头匹配的匹配.我想扩展这个也用斜线和括号后的单词.

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

我怀疑这只是通过向构造函数添加选项而变化,但我愿意更改/硬编码源脚本.

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

Did*_*hys 79

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

search_contains: true
Run Code Online (Sandbox Code Playgroud)

选项文档


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

如果您愿意更改插件源本身,这是一种方法.

在插件中进行搜索的方法是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');
Run Code Online (Sandbox Code Playgroud)

将其更改为:

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

DEMO


Las*_*ert 45

可以使用该选项设置搜索行为 search_contains

这是默认情况 false

将其设置为true,并选择也将在内部而不是仅在开头找到匹配项:

$('#my_dropdown').chosen({ search_contains: true });
Run Code Online (Sandbox Code Playgroud)


小智 7

与选择1.0一样,只需添加选项{search_contains:true}

$('.selector').chosen({search_contains: true});
Run Code Online (Sandbox Code Playgroud)

玩得开心.