gpt4 book ai didi

json - Jquery UI 自动完成 ajax 未填充下拉框

转载 作者:行者123 更新时间:2023-12-01 00:07:54 24 4
gpt4 key购买 nike

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

当我在 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}]

最佳答案

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

关于json - Jquery UI 自动完成 ajax 未填充下拉框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9481972/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com