【问题标题】:JQuery UI Autocomplete is not returning any results to the pageJQuery UI Autocomplete 没有向页面返回任何结果
【发布时间】:2014-11-07 13:02:14
【问题描述】:

我正在使用JQuery UI 自动完成小部件在用户在我的搜索栏中键入时向他们显示建议的搜索。但是,我遇到了一个问题,即在用户键入时结果不会出现在用户面前。

这是我的script

var $j = jQuery.noConflict();
    $j(document).ready(function(){  
        $j("#tag_search").autocomplete({                        
            source:'autocomplete_tags.php',
            dataType: 'json',
            minLength:1
    });
});

这是autocomplete_tags.php

require_once '../includes/preheader.php';//connects to db
$term = $_GET["term"];//Get the search terms. 

//Select tags from the database based on the specified search keyword
 $query  = "SELECT * FROM tag WHERE NAME LIKE '".$term."%' ORDER BY NAME ASC";
 $json=array();

 $db->query($query);//Query the DB.

 //Parse through the results and store in an array
 while($db->next()){
    $json[]=array(
            'id' => $db->get('id'),
            'value'=>$db->get("name"),
            'label'=>$db->get("description")
    );
 }
 //Autocomplete JQuery UI expects a Json Array so encode and echo. 
 echo json_encode($json);

如果我访问这个 url,我可以在浏览器中看到一个带有结果的 json 数组,这让我相信它正在正常工作,但由于某种原因,结果没有显示。

http://my-site.tld/includes/autocomplete_tags.php?term=ag

输出:

[{"id":"29","value":"Agriculture","label":"This is a tag about agriculture and farming"}]

另外,我在控制台中检查了当用户键入时正确找到了文件,并且收到了200 响应代码。

有谁知道这可能是什么原因造成的?我已经在另一个站点上实现了类似的功能,并且没有问题。会不会是造型问题?

【问题讨论】:

    标签: javascript php jquery jquery-ui jquery-ui-autocomplete


    【解决方案1】:

    好吧,我很愚蠢,没有意识到这一点,而不是删除这个问题,对于其他可能尝试做同样事情的人,您必须确保包含 jquery-ui css 文件。只需将其放在您网站的标题中:

    <link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.0/themes/smoothness/jquery-ui.css" />
    

    就我上面的帖子而言,所有代码都是正确的并且可以正常工作,唯一缺少的是上面的样式表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-12
      • 1970-01-01
      • 1970-01-01
      • 2014-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多