我有自動完成腳本的問題。腳本在輸入時不提供任何名稱。 這裏是指數:自動完成不會給出結果
<head>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Autocomplete - Multiple values</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
function getData(){
var userName = document.getElementById("tags");
var string = userName.value;
$.ajax({
method: 'POST',
url: 'bla.php',
data: {
str: string
},
success: function(content) {
console.log("Content: " + content);
var availableTags = content;
function split(val) {
return val.split(/,\s*/);
}
function extractLast(term) {
return split(term).pop();
}
$("#tags").autocomplete({
minLength: 0,
source: function(request, response) {
// delegate back to autocomplete, but extract the last term
response($.ui.autocomplete.filter(
availableTags, extractLast(request.term)));
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function(event, ui) {
var terms = split(this.value);
// remove the current input
terms.pop();
// add the selected item
terms.push(ui.item.value);
// add placeholder to get the comma-and-space at the end
terms.push("");
this.value = terms.join(", ");
return false;
}
});
},
error: function(xhr, status) {
console.log("ERROR");
}
});
}
</script>
</head>
<body>
<div class="ui-widget">
<label for="tags">Names: </label>
<input id="tags" size="50" onKeyDown="getData();">
</div>
</body>
</html>
這裏是bla.php 連接到數據庫是正確的,從數據庫數據是正確的。
$d = new Database();
$d->query("SELECT u.cele_jmeno, u.id FROM uzivatele u WHERE u.cele_jmeno LIKE CONCAT(:str,'%') ORDER BY u.cele_jmeno LIMIT 0,15");
$d->bind(":str", $_POST["str"]);
$vysledky = $d->resultset();
$res = Array();
$num = 0;
foreach($vysledky AS $vysledek){
$res[$num] = $vysledek["cele_jmeno"];
$num++;
}
echo json_encode($res);
在ajax我有控制檯日誌,哪個返回coorect值。例如,如果我inser輸入Ter,然後在console.log我有:內容:[「Terrence Rowell」,「Terry Moony」,「Terry Morco」],如果我插入特里我只有內容:[「Terry Moony」 ,「Terry Morco」]。這是完全正確的數據,但是,腳本沒有寫入自動填充框。什麼時候,我有javascrit數組中的數據,所有的作品,但如果我從PHP腳本插入數組我沒有自動填充框。
下面是截圖: Box when I write first letter
控制檯是否顯示任何錯誤?請修復您的帖子拼寫,以便使用戶更容易。 – Script47
我在控制檯中沒有任何錯誤。 –
在DOM下載入你的jQuery或者在'DOMContentLoaded' /'document.ready'中包裝,看看是否有幫助。 – Script47