Jquery自动完成列表

时间:2010-08-18 12:30:20

标签: jquery

我正在使用的jquery函数是:

$.getJSON("rpc2.php?queryString=" +inputString+"", function(data) { 
if(data.length >0) {
$.each(data, function(i, data){
var city= data.city;
var country= data.country;
$('#suggestions').show();
$('#autoSuggestionsList').html('<li>'+ city+'</li>');
 });
 }
 });

PHP看起来像:

if(strlen($queryString) >0) {
$query = "SELECT * FROM cities WHERE city_accented LIKE '$queryString%' LIMIT 5";
$result = mysql_query($query) or die("There is an error in database");
$json = array();
while($row = mysql_fetch_array($result)){
$json['city'] = $row['city_accented'];
$json['country'] = $row['country'];
$data[] = $json;  
}
}
print json_encode($data);

我从PHP得到的回应是:

[{"city":"Lors","country":"ad"},{"city":"Lo Serrat","country":"ad"},{"city":"Lobabi","country":"af"},{"city":"Lobya","country":"af"},{"city":"Locakan","country":"af"}]

问题是在autoSuggestionsList DIV中只列出了第一个城市,而不是全部五个来自PHP响应。为什么在autoSuggestionsList div中没有​​来自php响应的其他城市?

1 个答案:

答案 0 :(得分:3)

尝试:

$('#autoSuggestionsList').append('<li/>' + city + '</li>');