我输入了id aktForm_tiekejas并且是jquery自动完成代码:
$('#aktForm_tiekejas').autocomplete({
serviceUrl: '_tiekejas.php',
width: 185,
deferRequestBy: 0,
noCache: true,
onSelect: function(suggestion) {alert('You selected:'+suggestion.value+','+suggestion.data);}
});
_tiekejas.php:
<?php
include("../Setup.php");
$query = ($_GET['query']);
$reply = array();
$reply['query'] = $query;
$reply['suggestions'] = array();
$reply['data'] = array();
$res = mysql_query("SELECT id,pavadinimas FROM sarasas_tiekejas WHERE pavadinimas LIKE '%$query%' ORDER BY pavadinimas ASC");
while ($row = mysql_fetch_array($res)) {
$reply['suggestions'][] = $row['pavadinimas'];
$reply['data'][] = $row['id'];
}
mysql_close();
echo json_encode($reply);
?>
如果查询是'vac'php从服务器返回:
{"query":"vac","suggestions":["UAB Vivacitas"],"data":["866"]}
但是
alert('You selected:'+suggestion.value+','+suggestion.data);
不会提醒数据(866)
为什么呢?......
答案 0 :(得分:0)
可能是因为suggestion.value不存在。查看您的JSON响应代码,我可以看到suggestion.data但没有advicetion.value。由于JS将寻找不存在的值,因此会抛出错误。你也错过了返回第二部分的数组。试试这个:
alert('You selected: '+suggestion.data[0]);
如果您需要遍历数据子集,请执行以下操作:
for(i in suggestion.data){
alert('You selected: '+suggestion.data[i]);
}