Jquery UI自动完成ajax不会填充下拉框

时间:2012-02-28 12:33:37

标签: json jquery-ui jquery jquery-autocomplete

我需要帮助,我看不出问题出在哪里。

当我在html文件中设置自动完成源时,它工作正常,当我在ajax.php中打印出相同的源或数据库值并通过ajax返回它时它不起作用。可能是什么问题呢?请帮助。

Html:

    <!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Auto complete</title>
        <script type="text/javascript" src="jquery-1.7.1.min.js"></script>
        <script type="text/javascript" src="jquery-ui-1.8.18.custom.min.js"></script>
        <link rel="stylesheet" media="all" type="text/css" href="jquery-ui-1.8.custom.css" />
        <style type="text/css">
            .ui-autocomplete-loading {
                background: url("images/loader.gif") no-repeat scroll right center white;
            }
        </style>
        <script type="text/javascript">
            jQuery(document).ready(function($){                       
                $("#ac").autocomplete({
                    minLength: 2,
                    //source: [{"value":"Some Name","id":1},{"value":"Some Othername","id":2}]
                    source: function( request, response){
                        $.ajax({
                            type: 'GET',
                            url: 'ajax.php',
                            data: {
                                'term':request.term
                            },
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: function(data){
                                console.log('Success : ' + data);
                            },
                            error: function(message){
                                alert(message);
                            }
                        });
                    },
                    select: function( event, ui ) {
                    }
                });
            });
        </script>
    </head>
    <body>
        <input type="text" id="ac" name="ac" size="100" />
    </body>
</html>

和我的ajax.php文件:

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'test_db';

$server = mysql_connect($dbhost, $dbuser, $dbpass);
$connection = mysql_select_db($dbname, $server);

$term = $_GET['term'];

$qstring = "SELECT user_id,tName FROM `csv_data` WHERE tName LIKE '%" . $term . "%'";
$result = mysql_query($qstring);

$return_arr = array();

$i = 0;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {//loop through the retrieved values
    $row_a = array();
    if ($row['tName'] != null) {
        $i++;
        $row_a['value'] = ($row['tName']);
        $row_a['id'] = (int) $i;
        array_push($return_arr, $row_a);
    }
}

mysql_close($server);

header("Content-type: text/x-json");

/*$my_arr = array(
    array("value" => "Some Name", "id" => 1),
    array("value" => "Some Othername", "id" => 2)
);*/

//echo json_encode($return_arr);
print json_encode($return_arr);

//print json_encode($my_arr);

这是来自firebug的响应(从数据库生成)。

[{"value":"4 erste Spiele","id":1},{"value":"Meine ersten Spiele \"Blinde Kuh\"","id":2},{"value":"4 erste Spiele","id":3},{"value":"Meine ersten Spiele \"Blinde Kuh\"","id":4},{"value":"4 erste Spiele","id":5},{"value":"Meine ersten Spiele \"Blinde Kuh\"","id":6},{"value":"Maxi Kleine Spielewelt","id":7}]

1 个答案:

答案 0 :(得分:6)

参数response实际上是一个必须调用的回调 - 将数据传递给它 - 以显示结果弹出菜单。只需在“成功”回调中调用它:

source: function(request, response) {
    $.ajax({
        ...
        success: function(data) {
            // pass your data to the response callback
            response(data);
        },
        error: function(message) {
            // pass an empty array to close the menu if it was initially opened
            response([]);
        }
    });
},