简体   繁体   English

jQuery自动完成功能不显示返回的json数据

[英]JQuery autocomplete doesnt display returned json data

I've been struggling with this since yesterday. 从昨天开始,我一直在为此苦苦挣扎。 I successfully get the data from server in Json format but the data doesnt shown in the input box. 我成功以Json格式从服务器获取了数据,但数据未显示在输入框中。 在此处输入图片说明 I have checked similar questions and I think it is kind of format problem like this guy's question , but I couldnt solve it out. 我检查了类似的问题,我认为这是格式问题,就像这个人的问题一样 ,但是我无法解决。 I have tried many code examples and this was the least complicated one. 我尝试了许多代码示例,这是最简单的示例。

HTML form HTML表格

<form action="" method="post">
    <input type="text" placeholder="Name" id="customerAutocomp" class="ui-autocomplete-input" />
</form>

Javascript Java脚本

$(function() {
    $("#customerAutocomp").autocomplete({
        source: "suggest_name.php",
        minLength: 2,
        select: function(event, ui){
            var url = ui.value.id;
            if (url !== '#') {
                location.href = '/somepage.php?somecode=' + url;
            }
        },

        // optional
        html: true,

        // optional (if other layers overlap the autocomplete list)
        open: function(event, ui) {
            $(".ui-autocomplete").css("z-index", 1000);
        }
    });
});

JQuery Versions that I'm using 我正在使用的JQuery版本

<script src="//code.jquery.com/jquery-2.2.4.js">       </script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

PHP 的PHP

//connnections strings//
$conn = mysql_connect($host, $username, $password, $db_name) or die("Could not connect: " . mysql_error());
mysql_select_db("stackoverflow");
$term = trim(strip_tags($_GET['term']));

$qstring = "SELECT fullname as value,id FROM users WHERE fullname LIKE '%".$term."%'";
$result = mysql_query($qstring);

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
        $row['value']=htmlentities(stripslashes($row['value']));
        $row['id']=(int)$row['id'];
        $row_set[] = $row;
}
echo json_encode($row_set);

Please remove the script files returned in the JSON response. 请删除JSON响应中返回的脚本文件。

Hope it helps ! 希望能帮助到你 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM