jQuery UI自动完成ajax未填充下拉框 [英] Jquery UI autocomplete ajax is not populating dropdown box

查看:117
本文介绍了jQuery UI自动完成ajax未填充下拉框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need help with this, I can't see where is the problem.

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

When I set source for autocomplete in html file, it works fine, when I same source or database values print out in ajax.php and return it via ajax it doesn't work. What could be the problem? Help please.

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文件:

and my ajax.php file:

$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);

这是萤火虫(从数据库生成)的响应.

This is response from firebug(generated from database).

[{"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}]

推荐答案

参数response实际上是一个回调,您必须调用该回调-将数据传递给它-以显示结果弹出菜单.只需在成功"回调中调用它即可:

The parameter response is actually a callback tthat you have to call - passing it your data - to display the result popup menu. Simply call it in the "success" callback:

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([]);
        }
    });
},

这篇关于jQuery UI自动完成ajax未填充下拉框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆