jQuery UI 自动完成传递输入数据

发布于 2024-09-09 16:19:08 字数 582 浏览 2 评论 0原文

我确信这个问题已经被问过很多次了,但希望得到一些帮助。

我正在尝试设置 jQuery UI,我可以使用它从 JSON 获取静态结果列表。 但我需要将 INPUT 值传递到 PHP 脚本中,以便它可以实际过滤结果。

我的输入字段代码

<input id="search" />

我的运行 Javascript

$("#search").autocomplete({
   source: 'testData.php',
   dataType: 'json',
   minLength: 2,
   select: function(event, ui) {
            $('#contactId').val(ui.item.id);
            $('#contactName').val(ui.item.value);
   }
});

和 testData.php 的代码正在返回有效的 JSON 数据。 但我不知道如何将变量从输入字段传递到我的 testData.php,以便它实际上知道要搜索什么。

希望这是有道理的。

I am sure this has been asked a ton of times, but would appreciate some assistance.

I am trying to setup the jQuery UI, which I can get a static result list with from JSON.
But I need to pass my INPUT value onto the PHP script to that it can actually filter the results.

My Code for the Input Field

<input id="search" />

My Code for running my Javascript

$("#search").autocomplete({
   source: 'testData.php',
   dataType: 'json',
   minLength: 2,
   select: function(event, ui) {
            $('#contactId').val(ui.item.id);
            $('#contactName').val(ui.item.value);
   }
});

And testData.php is returning valid JSON data.
But I don't know how to pass the variable from the input field to my testData.php so that it actually knows what to search for.

Hope this makes sense.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

寄意 2024-09-16 16:19:08

您无需为此做任何事情。控件会自动为您传递该值。在你的 php 脚本中只需使用这个:

$_GET["term"]

它们通过术语名称传递一个查询字符串变量。它在文档中,但很难找到。

编辑:我知道这一点是因为我上周试图找到它时遇到了同样的问题。以下是文档的 URL:http://docs.jquery.com/UI/Autocomplete

另外,这里还有页面中解释要做什么的段落:

当使用字符串时,自动完成插件期望该字符串指向将返回 JSON 数据的 URL 资源。它可以位于同一主机或不同主机上(必须提供 JSONP)。请求参数“term”被添加到该 URL 中。数据本身可以采用与上述本地数据相同的格式。

You don't have to do anything for this. The control automatically passes the value for you. In your php script just use this:

$_GET["term"]

They pass a querystring variable by the name of term. It's in the docs but a little obscure to find.

EDIT: I knew this because I was having the same problem last week trying to find this. Here is the URL to the docs: http://docs.jquery.com/UI/Autocomplete

Also here is the paragraph from the page that explains what to do:

When a String is used, the Autocomplete plugin expects that string to point to a URL resource that will return JSON data. It can be on the same host or on a different one (must provide JSONP). The request parameter "term" gets added to that URL. The data itself can be in the same format as the local data described above.

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