【问题标题】:Autocomplete is returning undefined自动完成返回未定义
【发布时间】:2011-10-27 10:24:24
【问题描述】:

这是我的 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"}]

【问题讨论】:

  • 需要设置json文件类型头吗?

标签: jquery-ui autocomplete undefined


【解决方案1】:

我认为您只是希望您的 JSON 数组看起来像这样:

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

或者,如果您想使用一个对象,请提供 labelvalue(这是 UI 自动完成所期望的):

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

【讨论】:

    【解决方案2】:

    我已经添加了

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

    它奏效了……该死。 我希望可以回答自己的问题,哈哈。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-27
      • 2011-06-21
      相关资源
      最近更新 更多