简体   繁体   English

jQuery UI AutoComplete传递输入数据

[英]jQuery UI AutoComplete Passing input data

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. 我正在尝试设置jQuery UI,可以从JSON获取静态结果列表。 But I need to pass my INPUT value onto the PHP script to that it can actually filter the results. 但是我需要将我的INPUT值传递到PHP脚本中,以便它实际上可以过滤结果。

My Code for the Input Field 我的输入字段代码

<input id="search" />

My Code for running my Javascript 我的代码,用于运行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. 并且testData.php返回有效的JSON数据。 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. 但是我不知道如何将变量从输入字段传递到我的testData.php,以便它实际上知道要搜索的内容。

Hope this makes sense. 希望这是有道理的。

You don't have to do anything for this. 您无需为此做任何事情。 The control automatically passes the value for you. 控件会自动为您传递值。 In your php script just use this: 在您的PHP脚本中,只需使用以下命令:

$_GET["term"]

They pass a querystring variable by the name of term. 它们通过术语名称传递一个querystring变量。 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 这是文档的URL: 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. 使用String时,Autocomplete插件希望该字符串指向将返回JSON数据的URL资源。 It can be on the same host or on a different one (must provide JSONP). 它可以位于同一主机上,也可以位于不同的主机上(必须提供JSONP)。 The request parameter "term" gets added to that URL. 请求参数“ term”已添加到该URL。 The data itself can be in the same format as the local data described above. 数据本身可以与上述本地数据的格式相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM