jQuery UI - 从数据库自动完成
让 jQuery UI 的自动完成功能使用数据库的步骤是什么?
具体来说,如何向该脚本传递输入的值?自动完成如何接收脚本的 json?
我所知道的:
1)将“源选项”更改为查询数据库的脚本。
2) ?
当前代码:
$("#searchInput input").autocomplete({
source: "script_that_queries_the_db.php"
});
What are the steps for having jQuery UI's autocomplete use a database?
Specifically, how do I pass this script the entered value? How does autocomplete receive the script's json?
What I know:
1) Change the 'source option' to a script that queries the database.
2) ?
Current code:
$("#searchInput input").autocomplete({
source: "script_that_queries_the_db.php"
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第 2 步?让您的 PHP 页面
mysql_query
基于$_GET['term']
并使用json_encode
返回结果。编辑:另外,请确保传递给
json_encode
的数组是一个平面数组,否则 jQueryUI 将无法像我们希望的那样读取它,而无需编写更多自定义代码。step 2? Have your php page
mysql_query
based on$_GET['term']
and return the results usingjson_encode
.Edit: Also, make sure the array you pass to
json_encode
is a flat array, otherwise jQueryUI won't read it as well as we'd like without writing more custom code.最简单的方法是让服务器以 json 形式返回结果。看这个例子:
http://jqueryui.com/demos/autocomplete/#remote
另一种方法是通过传递函数作为源来自己发出请求并解析响应。看这个例子:
http://jqueryui.com/demos/autocomplete/#remote-with-cache
无论哪种情况,传递给自动完成的数据都必须是对象数组,每个对象都有一个标签和一个值。
The simplest way is to have your server return the results in json. See this example:
http://jqueryui.com/demos/autocomplete/#remote
Another way to do it is to make the request and parse the response yourself, by passing a function as source. See this example:
http://jqueryui.com/demos/autocomplete/#remote-with-cache
In either case the data passed to autocomplete must be an array of objects each with a label and a value.