0

我输入了 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)

为什么?...

4

1 回答 1

0

可能是因为Suggestion.value 不存在。查看您的 JSON 响应代码,我可以看到建议数据,但没有建议值。由于 JS 将寻找一个不存在的值,它会抛出一个错误。此外,您还缺少返回第二部分的数组。尝试这个:

alert('You selected: '+suggestion.data[0]); 

如果您需要遍历数据子集,请执行以下操作:

for(i in suggestion.data){
    alert('You selected: '+suggestion.data[i]);
}
于 2013-06-27T12:55:07.937 回答