如何在输入时过滤 jquery 自动完成数据
到目前为止,我有以下内容
var aCleanData = ['aaa','aab','faa','fff','ffb','fgh','mmm','maa'];
$('#my-input').autocomplete({
source:aCleanData,
minLength:2
});
当前如果您输入 aa
,将显示 aaa,aab,faa,maa
。
我想做的是,当用户输入 ff
时,显示的数据将是 fff,ffb
数据。
基本上,只有输入的内容才应该从第一个字符开始匹配。
这应该是递归的。当用户输入 fg
时,fff,ffb
应消失,仅应出现 fgh
。
预先感谢您的帮助。
更新:
ps 看看我的意思:
http://jqueryui.com/demos/autocomplete/#default< /a>
输入 sc
,您将看到的不仅仅是以 sc 开头的数据。
So far I have the following
var aCleanData = ['aaa','aab','faa','fff','ffb','fgh','mmm','maa'];
$('#my-input').autocomplete({
source:aCleanData,
minLength:2
});
Currently if you type aa
, aaa,aab,faa,maa
will show.
What I would like to do is when the user types ff
the data that is shown will be the fff,ffb
data.
Basically, only what is typed should be matched from the first character onwards.
This should be recursive. When user types fg
, fff,ffb
should disapear and only fgh
should appear.
Thanks in advance for your help.
UPDATE:
p.s. See what I mean here:
http://jqueryui.com/demos/autocomplete/#default
Type sc
and you'll see more than just the data beginning with sc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看自动完成的源代码后,您有几个选择。您可以编写自己的 pasrer 方法来返回您需要的内容并将其设置为回调源。这可能是更“正确”的方法。
更快的方法是在包含 ui 源代码后简单地添加以下行:
如果您关心它是如何工作的(或者应该工作,我没有测试过):
原始代码使用 2 个静态函数扩展了 ui.autocomplete:
所有你应该需要做的是更改 escapeRegex 返回的内容以仅搜索开头的单词。通过将 escapeRegex 的值设置为在原始返回前面返回
'[^|\s]'
,我们的意思是“查找前面有空格或者是行开头的工作” ”After looking at the source for autocomplete, you have a few options. You could write your own pasrer method that returns what you need and set it as the callback source. This is probably the more "correct" way to do it.
The faster way is to simply add the following line AFTER you include the ui source:
If you care how this works (or should work, I have not tested):
The original code extends the ui.autocomplete with 2 static functions:
All you should need to do is change what escapeRegex returns to search for the beginning if words only. By setting the value of escapeRegex to return
'[^|\s]'
in front of the original return, we are saying "Look for the work with a space in front or is the beginning of a line"我认为您使用jQuery UI - 自动完成。单一搜索类型与您想要的方式不同。但是这个可能可以满足您的需求。
I think you use the jQuery UI - Autocomplete. The one searching type is not the same way that you want to do. But this one is may be cover for your need.
您可以使用此技术的简单方法:
Simply way you can use this technique :
仅获取以输入值开头的结果的一种可能的解决方案是在自行搜索之前检查数组元素:
另请参阅我的 jsfiddle< /a>.
One possible solution to get only results starting with the input value is to check the array elements before search by yourself:
Also see my jsfiddle.