自动完成未返回undefined

时间:2011-10-27 02:01:49

标签: jquery-ui autocomplete undefined

这是我的jquery函数:

$(function() {
    $("#user").autocomplete({
        source: 'spiderman/null/users.php',
        minLength: 2
    });
});

当我开始在我的

中输入至少2个字母时
<input type="text" class="highlight" id="user" name="user"/>

显示项目列表“未定义”。 看起来它是一个无效的来源,但我很确定它是有效的,因为我在同一个目录中制作了include_once,并且加载了源脚本。

这是我的PHP代码:

if( !$_GET['term'] ) {
     die();
}

$return_arr = array();
$ac_term = "%".$_GET['term']."%";
$query = "SELECT `name` FROM `users` WHERE `name` LIKE :term";
$result = $conn->prepare($query);
$result->bindValue(":term", $ac_term);
$result->execute(); 

/* Retrieve and store in array the results of the query.*/
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
    $row_array['name'] = $row['name'];

    array_push($return_arr, $row_array);
}

/* Toss back results as json encoded array. */
echo json_encode($return_arr);

我也测试了PHP代码,它运行良好:对于术语lol,它确实返回了:

[{"name":"LolShit"},{"name":"Lolipop"},{"name":"Lolo"},{"name":"Lolololololololo"},{"name":"Loll"},{"name":"Pro Lol"}]

2 个答案:

答案 0 :(得分:0)

我想你或者只是希望你的JSON数组看起来像这样:

["LolShit","Lolipop","Lolo","Etc..."]

或者,如果您想使用对象,请提供labelvalue(UI自动填充预期):

[{"label":"LolShit","value":"LolShit"},{"label":"Lolipop","value":"Lolipop"},...]

答案 1 :(得分:0)

我添加了

$row_array['value'] = $row['name'];

它奏效了......该死的。 我希望能回答自己的问题,哈哈。