jQuery UI自动搜索以搜索多个字符串
我需要向jQuery UI自动搜索功能添加一些功能。我想解决的问题是,允许用户以任意顺序输入文本,从而搜索术语列表并显示建议。例如,假设我有以下术语:jQuery UI自动搜索以搜索多个字符串,jquery,jquery-ui,jquery-autocomplete,jquery-ui-autocomplete,Jquery,Jquery Ui,Jquery Autocomplete,Jquery Ui Autocomplete,我需要向jQuery UI自动搜索功能添加一些功能。我想解决的问题是,允许用户以任意顺序输入文本,从而搜索术语列表并显示建议。例如,假设我有以下术语: the brown cow jumped over the moon the blue cow watched in horror the red cow simply laughed the green cow got sick at such a sight the yellow cow lost 5 bucks to the black
the brown cow jumped over the moon
the blue cow watched in horror
the red cow simply laughed
the green cow got sick at such a sight
the yellow cow lost 5 bucks to the black cow
the black cow smiled at his fortune
If the user types in "the cow", I would expect the autocomplete feature to list all the results.
If I type in "brown moon", I would expect the first result to appear.
If I type in "fortune smiled", the last result would appear.
基本上,这种行为允许用户以任何顺序键入任何字符串并获得搜索结果
我是这么想的。我需要在“打开”或“搜索”事件中添加一个回调函数,并在那里操作结果。以下是我目前的代码:
$(function ()
{
var data =
[
"the brown cow jumped over the moon",
"the blue cow watched in horror",
"the red cow simply laughed ",
"the green cow got sick at such a sight",
"the yellow cow lost 5 bucks to the black cow",
"the black cow smiled at his fortune"
];
$(".text-search").autocomplete(
{
autoFocus: true,
source: data,
delay: 0,
minLength: 2,
open: function (e, ui)
{
debugger;
// what should i do here?
},
search: function (e, ui)
{
debugger;
// what should i do here?
}
});
});
<div class="ui-widget">
<label for="autocomplete">Autocomplete: </label>
<input class="text-search">
</div>
$(函数()
{
var数据=
[
“棕色的母牛跳过了月亮”,
“蓝母牛惊恐地看着”,
“红牛只是笑了”,
“绿母牛看到这样的景象就生病了”,
“黄牛输给了黑牛5块钱”,
“黑牛对他的命运微笑”
];
$(“.text search”).autocomplete(
{
自动对焦:对,
资料来源:数据,
延迟:0,
最小长度:2,
打开:功能(e、ui)
{
调试器;
//我在这里该怎么办?
},
搜索:功能(e、ui)
{
调试器;
//我在这里该怎么办?
}
});
});
自动完成:
我将根据用户输入的文本创建您自己的正则表达式。然后,可以使用此正则表达式测试候选列表中的每个项目:
$(".text-search").autocomplete({
autoFocus: true,
source: function(request, response) {
var regexStr = "\\b(" + $.map($.trim(request.term).split(" "), function(term) {
return $.ui.autocomplete.escapeRegex($.trim(term))
}).join("|") + ")\\b",
matcher = new RegExp(regexStr);
response($.grep(data, function(value) {
return matcher.test(value.label || value.value || value);
}));
},
delay: 0,
minLength: 2
});
正则表达式部分看起来很神秘,但它只是使用交替(|
)生成一个表达式。例如,如果键入brown cow
,将生成\b(brown | cow)\b
,它将匹配任何带有“brown”或“cow”的字符串
示例:您是在询问一种基于搜索条件进行语义搜索的方法,还是使用自动完成调用服务器url以获取结果并呈现结果?