【问题标题】:jquery-ui autocompletion does not work properlyjquery-ui 自动补全不能正常工作
【发布时间】:2011-10-21 19:06:19
【问题描述】:

我正在尝试从 php 文件中获取一些自动完成功能,但效果不佳..

这是 html 文件:

<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags" />
</div>

这是脚本

<script>
$(function() {

    $( "#tags" ).autocomplete({
        source: "test.php", 
        autoFocus : true,
        dataType: "json"

    });
});
</script>

还有 test.php 文件:

<?

 $term = $_REQUEST['term']; 

 $result = array();

 $arr['id'] = "pippo";
 $arr['value'] = "pippo";

 array_push($result, $arr);

 $arr['id'] = "topo";

 $arr['value'] = "topo";

 array_push($result, $arr);

echo json_encode($result);

 ?>

为什么我输入 T 我会同时得到 topopippo

【问题讨论】:

  • 您的远程数据源(PHP 代码)应该只返回与term 请求变量匹配的项目,而不是全部。

标签: jquery jquery-ui


【解决方案1】:

因为你是在告诉服务器返回所有结果:

 $arr['id'] = "pippo";
 $arr['value'] = "pippo";    
 array_push($result, $arr);    
 $arr['id'] = "topo";    
 $arr['value'] = "topo";    
 array_push($result, $arr);

不仅仅是那些匹配的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-19
    • 1970-01-01
    • 2017-05-08
    相关资源
    最近更新 更多