我正在使用devbridge jquery自动完成插件来创建自动完成搜索。我已经能够让它工作除了onSelect部分。
我正在返回正确的JSON编码数据(参见下文 - 从firebug复制)
{"query":"pa",
"suggestions":["Acer palmatum 'Bloodgood'","Quercus palustris","Euonymous elata 'Compactus'", "Parthenocissus tricuspidata"],
"data":[4,5,7,10]}
如上所述,一切正常,所有建议都显示出来并且可以选择等,但是当我选择一个项目时,会弹出警告消息
you selected: undefined, null
JQUERY CODE
$("#query").autocomplete({
serviceUrl:'includes/search.php',
width:350,
minChars:2,
onSelect: function(suggestion){ alert('You selected: ' + suggestion.suggestions + ', ' + suggestion.data); }
});
最后,PHP代码
include ("connect.inc.php");
$term = trim(strip_tags($_GET['query']));
$term = mysql_real_escape_string($term);
$qstring = "SELECT botanicalName, plantID FROM plants WHERE botanicalName LIKE '%$term%'";
$result = mysql_query($qstring);
$reply = array();
$reply['query'] = $term;
$reply['suggestions'] = array();
$reply['data'] = array();
while ($row = mysql_fetch_array($result)){
$reply['suggestions'][]=htmlentities(stripslashes($row['botanicalName']));
$reply['data'][]=(int)$row['plantID'];
}
echo json_encode($reply);
一如既往地感谢。
答案 0 :(得分:0)
我给你唯一的JS部分。你可以试试这个吗?
$("#query").autocomplete({
serviceUrl:'includes/search.php',
width:350,
minChars:2,
select: function(event, ui){ alert('You selected: ' + ui.item.suggestions + ', ' + ui.item.suggestions.data); }
});