我正在尝试创建一个自动完成输入框,当您单击内部键入时,将从数据库加载自动完成单词列表。我已经通过AJAX调用从from数据库加载了word list,但是当我将它传递给autocomplete函数时,它开始返回404。
我不知道为什么数据在我的警报中显示得很好,但不会自动完成吗?
$(document).ready(function() {
$("#tags").click(function() {
$.ajax({
url: 'get_players.php',
success: function(data){
alert(data); // This returns the correct string.
$("#tags").autocomplete({
source: data
});
}
});
});
}); // END Document.ready.更新:数据作为字符串返回(我认为)。我想使用这个足球选秀节目,所以数据是一个球员名称的名单,由逗号分隔。示例:
'Andy Dalton - QB Bengals', 'Adrian Peterson - RB Vikings', 'Tom Brady - QB Patriots'这是我的get_players.php
<?php
//Connect to the Database.
$mysqli = new mysqli("localhost","username","password", "draftboard");
// Check said connection.
if ($mysqli->connect_errno)
{
print "An error has occured.";
}
// Get all the players.
$result = $mysqli->query("SELECT * FROM players");
if($result->num_rows > 0) {
// For each member of the group...
for($i = 0; $i < $result->num_rows; $i++)
{
$row = $result->fetch_assoc();
echo "'" .$row['name'] . " - " . $row['position'] . " " . $row['team']."', ";
}
$result->close();
}
?>发布于 2013-07-30 21:00:55
autocomplete需要一个url、数组或函数。您当前正在传递一个字符串,所以只需使用它将其隐藏到一个数组中:
$("#tags").autocomplete({
source: data.split(",")
});https://stackoverflow.com/questions/17956562
复制相似问题