jQuery自动完成未定义


jQuery autocomplete undefined

我正在尝试编写一个脚本,该脚本将从数据库中插入数据。我有以下代码,但它不适用于我

<?php 
    $connection = new mysqli("localhost", "username", "password", "movies");
    if($connection->error) {
        die("I couldn't make the database connection");
    }
    $data = $connection->query("SELECT `id`, `title` FROM `titles`");
    if($data->num_rows > 0) { 
        $results = array();
        while($rows = $data->fetch_assoc()) {
            $results[] = array($rows["id"], $rows["title"]);
        }
    }
    header("Content-Type: application/json"); 
    echo json_encode($results); 
?>

使用以下HTML代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>jQuery UI Autocomplete functionality</title>
    <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">
    <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
    <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
    <script>
        $(function() {
            $( "#autocomplete1" ).autocomplete({
                source:"data.php",
                select: function( event, ui ) {
                $( "#autocomplete1" ).val( ui.item.code + " - " + ui.item.label );
                return false;
            }
        }).data( "autocomplete" )._renderItem = function( ul, item ) {
        return $( "<li></li>" )
            .data( "item.autocomplete", item )
            .append( "<a>" + item.code + " - " + item.label + "</a>" )
            .appendTo( ul );
            };      
        });        
    </script>
</head>
<body>
    <input type="text" id="autocomplete1">
</body>
</html>

然而,当我点击文本框时,我会得到一个空框,当我单击其中的某个内容时,当我应该从数据库中取回行时,我只会在框中显示"undefined-undefined"

你的mySQL查询对我来说很奇怪。试试这个。还可以使用var_dump()查看您是否从查询中得到了任何信息。从顶部开始,一路向下。在firebug中实际看到DOM错误之前,这不是JS问题。

<?php
 $query = mysql_query("SELECT * from schema.table_name ") or die(mysql_error());
    }
    $results= array();
    while($field = mysql_fetch_assoc($query)){
    //set variables here or in your case insert values into array..
        $id = $field['id'];
        $title = $field['titles'];
       $results[] = $title;
    }//close while loop
var_dump($restults); //lest see if anything even comes back.
die;//  i always die after this because who cares about the below until the above works.
?>

首先尝试这个:

$( document ).ready(function() {
            $( "#autocomplete1" ).autocomplete({
                source:"data.php",
                select: function( event, ui ) {
                $( "#autocomplete1" ).val( ui.item.code + " - " + ui.item.label );
                return false;
            }
        }).data( "autocomplete" )._renderItem = function( ul, item ) {
        return $( "<li></li>" )
            .data( "item.autocomplete", item )
            .append( "<a>" + item.code + " - " + item.label + "</a>" )
            .appendTo( ul );
            };      
        });

或者将脚本移动到<input type="text" id="autocomplete1"> 下方

$(function () {
        $("#autocomplete1").autocomplete({
            source: "data.php",
            select: function (event, ui) {
                $("#autocomplete1").val(ui.item.id+ " - " + ui.item.title);
                return false;
            }
        }).data("autocomplete")._renderItem = function (ul, item) {
            return $("<li></li>")
                .data("item.autocomplete", item)
                .append("<a>" + item.id+ " - " + item.title+ "</a>")
                .appendTo(ul);
        };
    });

如果$results数组以results=[{id:",title:"}]的格式存储数据,那么上面的代码就可以完成这项工作。