jQuery自动完成示例

你在哪里下载这个例子?
http://jqueryui.com/demos/autocomplete/#remote

jQuery没有使PHP可用,所以我不知道如何构造数据或如何使用它。

我没有看到页面上的下载按钮或有关如何构建PHP以与jQuery UI集成的任何说明,我不确定为什么他们的网站不容易让开发人员使用。

这是一个使用自动完成的常规aspx的Page_Load示例。 GetSCACs方法只返回表示JSON数组的字符串。

protected void Page_Load(object sender, EventArgs e) { // Clear out the buffer Response.ClearHeaders(); Response.ClearContent(); Response.Clear(); // Do not cache response Response.Cache.SetCacheability(HttpCacheability.NoCache); // Set the content type and encoding for JSON Response.ContentType = "application/json"; Response.ContentEncoding = Encoding.UTF8; string query = Request["term"]; string scacs = GetSCACs(query); Response.Write(scacs); // Flush the response buffer Response.Flush(); // Complete the request. NOTE: Do not use Response.End() here, // because it throws a ThreadAbortException, which cannot be caught! HttpContext.Current.ApplicationInstance.CompleteRequest(); } 

从概述:

数据源可以是:

  • 包含本地数据的数组
  • 一个String,指定一个URL
  • 回调

在这种特定情况下,设置使用第二个选项:

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

所以,它并不重要,但这里是search.php返回的JSON。 这应该满足您的抱怨“我不知道如何构建数据或如何使用它”。

 [ { "id": "Ficedula hypoleuca", "label": "Eurasian Pied Flycatcher", "value": "Eurasian Pied Flycatcher" }, { "id": "Muscicapa striata", "label": "Spotted Flycatcher", "value": "Spotted Flycatcher" }, { "id": "Branta canadensis", "label": "Greater Canada Goose", "value": "Greater Canada Goose" }, { "id": "Haematopus ostralegus", "label": "Eurasian Oystercatcher", "value": "Eurasian Oystercatcher" }, { "id": "Aythya marila", "label": "Greater Scaup", "value": "Greater Scaup" }, { "id": "Corvus corone", "label": "Carrion Crow", "value": "Carrion Crow" }, { "id": "Sylvia atricapilla", "label": "Blackcap", "value": "Blackcap" }, { "id": "Hydroprogne caspia", "label": "Caspian Tern", "value": "Caspian Tern" }, { "id": "Bubulcus ibis", "label": "Cattle Egret", "value": "Cattle Egret" }, { "id": "Aythya valisineria", "label": "Canvasback", "value": "Canvasback" }, { "id": "Aythya affinis", "label": "Lesser Scaup", "value": "Lesser Scaup" }, { "id": "Anas falcata", "label": "Falcated Duck", "value": "Falcated Duck" } ] 

但至于“如何”……让PHP输出一个JSON字符串,只需使用json_encode($arr)就像:

 $arr = array( "foo" => "bar", "baz" => "true", "thinger" => "thing" ); 

完整参考: http : //nz.php.net/json_encode

我想我找到了很好的资源
http://net.tutsplus.com/tutorials/javascript-ajax/how-to-use-the-jquery-ui-autocomplete-widget/

这是该网站的文件

  $row["name"]); } //echo JSON to page $response = $_GET["callback"] . "(" . json_encode($friends) . ")"; echo $response; mysql_close($server); ?>