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

时间:2012-02-14 09:20:42

标签: jquery jquery-plugins

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

例如: 搜索字符串:“第二个”与“第一个/第二个”或“第一个(第二个)”项不匹配。

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

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

5 个答案:

答案 0 :(得分:78)

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

search_contains: true

Options documentation


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

如果您愿意更改插件源本身,可以使用以下方法。

在插件中进行搜索的方法是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');

将其更改为:

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

<强> DEMO

答案 1 :(得分:45)

可以使用选项search_contains

设置搜索行为

默认为false

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

$('#my_dropdown').chosen({ search_contains: true });

答案 2 :(得分:7)

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

即可
$('.selector').chosen({search_contains: true});

玩得开心。

答案 3 :(得分:0)

在我选择的1.0中,我在第301行和第302行

escapedSearchText = searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
regexAnchor = "";

答案 4 :(得分:0)

可以使用选项search_contains来搜索选项中的子字符串,并可以用作:

$(".chosen-select").chosen({
    search_contains: true
});
相关问题