简体   繁体   English

jQuery的自动完成不返回建议数据onselec

[英]jquery autocomplete dont return suggestions data onselec

I have input with id aktForm_tiekejas and is jquery autocomplete code: 我输入的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: _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);
?>

If query is 'vac' php returns from server: 如果查询为“ vac”,则php从服务器返回:

{"query":"vac","suggestions":["UAB Vivacitas"],"data":["866"]}

but

alert('You selected:'+suggestion.value+','+suggestion.data); 

doesn't alert data (866) 不提醒数据(866)

why?... 为什么?...

Probably because suggestion.value doesn't exist. 可能是因为proposal.value不存在。 Looking at your JSON response code I can see the suggestion.data but no suggestion.value. 查看您的JSON响应代码,我可以看到uggestination.data,但没有uggestination.value。 Since JS will be looking for a value that doesn't exist it will throw an error. 由于JS将查找不存在的值,因此将引发错误。 Also you're missing out the array for the second part of the return. 另外,您会错过返回的第二部分的数组。 Try this: 尝试这个:

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

If you need to iterate through your data subsets do something like: 如果您需要遍历数据子集,请执行以下操作:

for(i in suggestion.data){
    alert('You selected: '+suggestion.data[i]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM