使用ajax输入位置时优化搜索结果?
我在 asp.net webform 页面中有这个文本框,用于输入城市。输入一些文本时,它会提供建议,就像 Facebook 提供的匹配结果一样。
我尝试了这两种方法来实现这一点。
- 我首先使用 onTextChanged 事件和 AJAX,发现它只有在文本框失去焦点时才有效。我想要一个在你打字时工作的解决方案。使用它的优点是我可以使用数据库并且速度会很快,因为在此过程中不会传输 xml 文件。
2.我用的是ajax,客户端用的是js。但问题是包含城市、州、国家的 xml 是一个巨大的 30MB 文件。因此,不可能使用它,因此考虑将每个字母表制作 26 个小 xml 文件,但它们仍然足够大以实际使用。所以,现在我计划使用 26*26 文件,其中包含具有相同前两个字母的城市,但我认为这是实现我想要的效果的无效方法。
还有其他有效的方法来实现它吗? 如果可以的话,最好的方法是使用数据库。
I have this textbox in asp.net webform page used to enter a city. On entering some text it provides suggestions just like facebook does of matching results.
I tried these two methods to implement this.
- I first used onTextChanged event and AJAX and found out it only works when the textbox loses focus. I wanted a solution to work as you type. Advantage of using this was that I could use a database and it would be fast, because no xml files will be transferred in the process.
2.I used ajax, clientside using js. But the problem is the xml containing cities, there states, country is a massive 30MB file. So, it was impossible to use it, so thought of making 26 small xml files of each alphabet out of that big one but still they would be big enough to actually use. So, now I am planning to use 26*26 files containing the cities with same first two alphabets but I think its ineffective way to do what I want.
Is there any other efficient way of accomplishing it?
The best way would be to use a database, if I could.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用 onkeypress 和/或 onkeyup 事件。
您是否知道有免费的即插即用自动完成组件?例如 http://jqueryui.com/demos/autocomplete/
使用 JSON!它更加紧凑。您可能会节省 30-40% 的数据大小。
您是否知道您不需要传递整个数据集即可正常工作?您可以将其保存在服务器上(例如,在数据库中,或缓存在网络服务器上,以加快访问速度并减少数据库流量),并让客户端根据其输入的字符一次仅提取一小部分数据。 JQuery UI AutoComplete 支持该功能。
如果您无法使用 JQuery 和 JQuery UI(不想要将是一个不可接受的答案),那么我很确定还有其他免费替代品,包括这个: http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/AutoComplete/AutoComplete.aspx
You need to use onkeypress and/or onkeyup events instead.
Did you know that there are plug-and-play auto-complete components out there that are free? For example http://jqueryui.com/demos/autocomplete/
Use JSON! It's much more compact. You'll probably save 30-40% on the size of that data.
Did you know that you don't need to pass the whole data set for that to work? You can have it live on the server (e.g. in the database, or cached on the webserver for faster access and less db traffic), and have clients only pull small set of data at a time, based on characters that they type. That JQuery UI AutoComplete supports that feature.
If you cannot use JQuery and JQuery UI (not wanting would be an unacceptable answer), then I'm pretty sure there are other free alternatives, including this one: http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/AutoComplete/AutoComplete.aspx