遇到jQuery UI自动完成问题

我正在尝试在jQuery UI中使用新的自动完成function ,但我遇到了一些问题。

我能够从数据库中检索数据(我可以在FireBug中看到它),但是我无法显示下拉列表(或警告数据)。

这是我的jQuery代码:

jQuery('#brand_search').autocomplete({ source: "http://mysite.com/wp-content/themes/storelocator/include/jquery.search.php?instance=brand", minLength: 2, delay: 50, select: function(e, ui) { alert(ui); } }); 

这是我的PHP代码:

 /* ------------------ Brand Autosuggest ------------------------- */ function autosuggestBrand($dal) { $result = $dal->getRowBySearch('sl_label','name', $this->term); $brands = array(); if(mysql_num_rows($result)>0) { while($row = mysql_fetch_assoc($result)) { array_push($brands, array( "id" => $row['id'], "name" => html_entity_decode($row['name'], ENT_QUOTES, 'UTF-8') ) ); } } echo json_encode($brands); } 

我看到这两个指南:
http://www.jensbits.com/2010/03/29/jquery-ui-autocomplete-widget-with-php-and-mysql
http://net.tutsplus.com/tutorials/javascript-ajax/how-to-use-the-jquery-ui-autocomplete-widget

但仍然无法弄清楚如何显示/提醒所提取的数据。

这是echo json_encode的结果

 [ {"id":"4642","name":"Mo Koshji"}, {"id":"4627","name":"MO-A"}, {"id":"4626","name":"MO'CYCLE"}, {"id":"4628","name":"mo851"}, {"id":"4629","name":"Mob Action"} ] 

使用以下内容更正您的php数组,以便为​​jquery-autocomplete获取正确的json输出:

 array_push ( $brands, array ( "label" => $row['id'], "value" => html_entity_decode($row['name'], ENT_QUOTES, 'UTF-8') ) ); ); 

因为jquery autocomplete需要那些json属性名来运行文档中指定的自动完成:

本地数据可以是一个简单的字符串数组,也可以包含数组中每个项目的对象,带有标签或值属性或两者。 label属性显示在建议菜单中。

http://jqueryui.com/demos/autocomplete/#custom-data

将您的代码更改为此(将其从click事件处理程序中取出):

 jQuery(function() { jQuery('#brand_search').autocomplete({ source: "http://mysite.com/wp-content/themes/storelocator/include/jquery.search.php?instance=brand", minLength: 2, delay: 50, select: function(e, ui) { alert(ui); } }); });