jQuery 自动完成填充数组
我正在尝试使用 jQuery 的自动完成功能,在阅读了几篇文章后,我仍然有两个问题:
1)我已经获得了自动完成功能,可以使用底部发布的代码,但是我需要填充标题为“data”的数组来自我们的数据库。我一直在尝试使用不同的方法通过 AJAX 来填充此内容。我尝试使用 $.get 和 $.ajax。完成此操作的正确语法是什么?
2)这个数组会很大,如果我只填充一次数组,我就会有60,000多个值。我想知道是否可以在每次用户输入新字母时执行 AJAX 请求来填充数组?这样做更好,还是一次性用所有值填充数组?哪个更好,哪个对系统的税收更少?
//This code works
<script type="text/javascript">
$(document).ready(function(){
var data = "Facebook Gowalla Foursquare".split(" ");
$("#search_company").autocomplete(data);
});
</script>
//display company live search
echo('<form id="form" method="post" action="competitor_unlink.php" onsubmit="return">');
echo('Company: <input id="search_company"/>');
echo('<br/>');
echo('<button type="submit" value="Submit">Submit</button>');
echo('</form>');
I'm attempting to use jQuery's autocomplete feature, and after reading several posts I still have two questions:
1) I've gotten autocomplete to work with the code posted at the bottom, however I need the array titled "data" to be filled from our database. I've been trying to use different methods to fill this via AJAX. I tried using $.get and $.ajax. What is the correct syntax to accomplish this?
2) This array will be big, I will have 60,000 plus values if I just fill the array once. I was wondering if it's possible to perform an AJAX request to fill the array every-time the user enters a new letter? Is this better to do, or just fill the array with all values at once? By better, which taxes the system less?
//This code works
<script type="text/javascript">
$(document).ready(function(){
var data = "Facebook Gowalla Foursquare".split(" ");
$("#search_company").autocomplete(data);
});
</script>
//display company live search
echo('<form id="form" method="post" action="competitor_unlink.php" onsubmit="return">');
echo('Company: <input id="search_company"/>');
echo('<br/>');
echo('<button type="submit" value="Submit">Submit</button>');
echo('</form>');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看看这个演示 - 这就是您想要做的(使用 ajax 获取数据):
http:// /jqueryui.com/demos/autocomplete/#remote
Look at this demo - it's what you want to do (get data using ajax):
http://jqueryui.com/demos/autocomplete/#remote
以下示例展示了如何使用 jQuery UI 自动完成插件指定一个 URL,该 URL 将以 JSON 形式从数据库返回结果。
自动完成功能会自动将名为“term”的查询字符串参数附加到 URL,因此您的搜索页面需要预料到这一点。不确定您正在使用什么服务器技术,但由于我是一名 .NET 开发人员,这里有一个 ASP.NET MVC 中的示例:)
jQueryUIAutoCompleteItem 只是一个数据容器,表示自动完成插件所需的 JSON 格式。
Here's an example of how to specify a URL that will return the results from the database as JSON using the jQuery UI autocomplete plugin.
Autocomplete will automatically append a querystring parameter named "term" to the URL so your search page will need to expect that. Not sure what server technology you're using but since I'm a .NET developer here's an example in ASP.NET MVC :)
jQueryUIAutoCompleteItem is just a data container that represents the JSON format that the autocomplete plugin expects.
您是对的,将整个 60,000 条记录列表发送到客户端的计算机听起来并不是最好的解决方案。您会注意到,与许多其他网站一样,谷歌在其自动完成功能中仅向您显示少数最受欢迎的匹配项。
您可以通过等待用户输入两个或三个字母而不是搜索第一个字母来缩短列表。
您可以在列表中进行页面分块(它有不同的名称)。也就是说,只返回前 10 或 15 个匹配项。用户可以通过滚动或单击“显示更多结果”链接来获取更多列表。当然,您必须为此编写(或搜索)所有 javascript 代码。
You're correct that sending the whole 60,000-record list to the client's machine doesn't sound like the best solution. You'll notice that Google only shows you a handful of the most popular matches in its autocomplete, as to many other websites.
You could shorten the list by waiting for the user to type two or three letters instead of searching on the first one.
You could do page chunking in the list (it goes by various names). That is, only return the top 10 or 15 matches. The user can get more of the list by scrolling or by clicking on a "Show More Results" link. You have to write (or search for) all the javascript code for this, of course.
这篇文章可能有点晚了,但对于其他找到它的人来说。我创建了一个 jquery 插件和与 Foursquare 一起使用的 jqueryui 自动完成控件。您可以阅读这篇文章并下载插件 Foursquare自动完成插件
It might be a bit late to this post but for others that find it. I created a plugin for jquery and the jqueryui autocomplete control that works with Foursquare. You can read the post and download the plugin at Foursquare Autocomplete Plugin