更新一个JQuery自动完成问题
Update a JQuery autocomplete issue
通过Stack Overflow搜索,我发现我需要的确切问题已经得到了回答。唯一的问题是它回答了一个被贬低的JQuery自动完成小部件。jquery-autocomplete插件搜索
本质上,我需要用JQuery Autocomplete做的是在数据库中找到所有的搜索词,并以任何顺序找到它们。例如,如果我们有一个像这样的数据库:
var availableTags = [
"apple is good",
"apple grows on tree",
"the tree and the apple",
"red apple",
"apple tree"
];
我们搜索"apple tree",我们会得到这个:
"苹果长在树上",
《树和苹果》,
"苹果树",
我希望这足够清楚!
我建议使用JQueryUI的自动补全功能:http://jqueryui.com/autocomplete/
它很容易使用和相当强大。我想它能满足你的需要。
否则,你可以自己创建你自己的自动完成功能。为此,只需使用简单的正则表达式:对于每个输入单词,测试是否在源数据中找到单词。如果只有一个匹配项,则将数据附加到结果中。
下面是使用RegExp的JS代码示例:// Here your tags
var availableTags = [
"apple is good",
"amazing apple"
// ...
];
// The main function
// $target is the input field
function autocomplete($target) {
var outcome;
var words;
var input;
var tmp;
outcome = new Array(); // wraps tags which match autcompletion
words = new Array(); // wraps all words from your input
input = $target.val().trim(); // input value
if (input === '') {
// No words, do nothing
return outcome;
}
// First step: browse input to extract all
// words
tmp = '';
for (var i = 0; i < input.length; i++) {
var current = input[i];
if (current === ' ') {
words.push(tmp);
tmp = '';
} else {
tmp += current;
}
}
// Do no forget pending word
words.push(tmp);
// Second step: browse each checked-in tag
// and test if all provided words are found
// in it
for (var i = 0; i < availableTags.length; i++) {
var isCancelled = false;
var j = 0;
var current = availableTags[i];
while (j < words.length && !isCancelled) {
var r = new RegExp(words[j], 'gi');
if (r.test(current)) {
// Ok word was found, go ahead
j++;
} else {
// Word was not here. You do not
// need to go ahead because you
// want to find all words in
// your tag
isCancelled = true;
}
}
if (!isCancelled) {
// If operation was not cancelled,
// save tag
outcome.push(current);
}
}
return outcome;
}
相关文章:
- 无法从 jQuery RSS Feed 中的 localStorage 动态替换类
- 如何使用jQuery自动打开页面上的所有链接
- 如何使用jquery在填充自动完成的值后使文本框只读
- jQuery:循环一个具有不同超时值的循环
- 用程序搜索JQuery数据表中的文本
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- jQuery匹配JSON对象的部分文本
- Jquery POST未填充数组
- JQuery使计数器每次更改时都会增加
- 如何在Angular2中使用jQuery插件
- 提高JQuery的性能
- 无法在通过jQuery的ajax加载的页面中执行javascript
- JQuery合并了keyup和focusout两个函数
- 如何使用jQuery选择下拉列表的值
- 将PHP变量传递给jQuery时遇到问题
- jQuery UI自动完成突然停止工作
- 剑道网格jQuery动画()问题
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- jquery点击函数select&取消选择
- jquery试图按名称获取按钮位置