JavaScript 在输入时自动完成
对了,我发现的所有自动完成插件和功能,它们仅在按键上/下键等时更新。这很好,但搜索仅在用户停止键入后才开始发生,并且如果他们正在键入短语或单词,则脚本无法立即开始建议等。
我知道这对于你们中的一些人来说将是一个非常简单的修复或建议,因此任何帮助将不胜感激,我将如何将其转换为按下按键时立即启动。
所需效果的一个示例是 Google Suggest 或 Facebook 搜索,每次按键或更改都会立即触发搜索,我如何模拟这一点?
谢谢!
Right guys, all autocomplete plugins and functions that I've found, they only update upon keyup/down, etc. This is fine but the search only begins occurring once the user has stopped typing and if they are typing a phrase or word, the script is unable to instantly start suggesting, etc.
I know this'll be a very simple fix or suggestion for some of you guys, so any help would be greatly appreciated as to how I can convert it to be instantly as a key is pressed.
An example of the desired effect is Google Suggest or Facebook search, a search is fired instantly per key press or change, how do I emulate this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你是这个意思吗?或者您希望 Ajax 从数据库中检索?
JQuery
编辑:我不确定我知道你的意思,因为这个例子似乎有效与 Google Suggest 或 Facebook 相同。如果您的数据库很小,您可以在页面加载时将缓存下载到变量数据中。如果您的数据库稍大一些,您可以将缓存限制为每个字母字符或一系列字符的响应数量。 (即,WHERE city LIKE 'aa%' LIMIT 10 AND WHERE...)
Is this what you mean? Or do you want Ajax to retrieve from a database?
JQuery
Edit: I'm not sure I know what you mean, because this example seems to work identical to Google Suggest or Facebook. If your database was small you could download the cache into the variable data upon page load. If your database was slightly larger you LIMIT the cache to only X number of responses for each alphabetical character or series of characters. (ie. WHERE city LIKE 'aa%' LIMIT 10 AND WHERE...)
这取决于您搜索的空间有多大以及您的服务器有多好。 Facebook 搜索(我假设是人名)很快,因为您实际上只是搜索一千个左右的联系人。谷歌速度很快,因为他们在基础设施上投入了大量资金并缓存了大量响应。
在我的一个项目中,我使用了 这个 jQuery 插件,它在缓存上提供了出色的性能结果。我们用它来提供大约 6K 联系人列表(姓名等)的自动完成功能。这就是你的想法吗?
It depends on how big the space you're searching is and how good your servers are. Facebook search for (I assume people's names) is quick because you're only really searching through a thousand or so contacts. Google is fast because they invest a lot of money in infrastructure and cache a lot of the responses.
On one of my projects I've used this jQuery plugin and it provides excellent performance on cached results. We used it to provide autocomplete functionality on a list of about 6K contacts (names, etc). Is this what you had in mind?
Wicket Web 框架具有“限制”行为的概念。通常,Wicket 应用程序中的 AJAX 请求会根据“ajax 通道”排队,如果没有正在运行,该通道会立即触发请求。如果请求已在运行,则下一个请求将排队,并在当前请求返回时触发。
“限制”让行为本身延迟一定的时间(例如两秒)。如果该行为在同一时间段内再次触发,则最近行为的回调将替换当前排队行为的回调。 (例如,用户开始输入“albuquerque”,这会触发事件“A”,然后是“AL”,然后是“ALB”。系统可能会触发“A”,然后是“ALB”,跳过“AL”,因为它是在队列中替换为“ALB”。)这样做的目的是在每次按键时立即触发一个行为,但防止服务器被不必要的请求淹没。
查看检票口ajax源代码:
https://github.com/apache/wicket/blob/wicket-1.4.8/wicket/src/main/java/org/apache/wicket/ajax/wicket-ajax.js
对于有关 Web 框架的更多信息,请参阅:
http://wicket.apache.org
The Wicket web framework has the concept of a "throttling" behavior. Normally, AJAX requests in Wicket applications are queued against an "ajax channel", which triggers a request instantly if none is running. If a request is already running, the next request is queued, and triggered when the current one returns.
"Throttling" lets the behavior delay itself for a certain amount of time (say, two seconds). If the behavior fires again in the same period, the callback for the most recent behavior replaces the callback for the current queued behavior. (For example, the user starts typing "albuquerque", which triggers the events "A" then "AL", then "ALB". The system might trigger "A", then "ALB", skipping over "AL" because it was replaced by "ALB" while sitting in the queue.) The object of this is to fire a behavior instantly on each keypress, but prevent the server from being flooded with unnecessary requests.
Check out the wicket ajax source code:
https://github.com/apache/wicket/blob/wicket-1.4.8/wicket/src/main/java/org/apache/wicket/ajax/wicket-ajax.js
For more about the web framework, see:
http://wicket.apache.org