jQuery UI - 从数据库自动完成

发布于 2025-01-07 13:44:20 字数 265 浏览 1 评论 0原文

让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

层林尽染 2025-01-14 13:44:20

第 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 using json_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.

清风无影 2025-01-14 13:44:20

最简单的方法是让服务器以 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文