我有這個輸入,我想用下面的腳本自動完成。url 回傳一個字串串列。當我輸入時,資料顯示在控制臺中,但不會彈出自動完成視窗。
可能有什么問題?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<input type="text" class="form-control my-input" name="from" id="from">
<script>
$(document).ready(function () {
$("#from").keyup(function (string) {
$.ajax({
type: "GET",
url: "http://127.0.0.1:5000/complete?station=" $(this).val(),
success: function (data) {
$("#from").autocomplete({
source: data
});
console.log(data)
}
});
});
});
</script>
</body>
</html>
uj5u.com熱心網友回復:
您應該將jQuery-ui.min.css,jquery.min.js和jquery-ui.min.js檔案添加到專案中。我使用模擬資料開發的以下應用程式成功運行。我呼叫了getList()錯誤欄位中的方法來模擬來自服務器的回應。
應用程式測驗影像如下:

應用程式源代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<title>Document</title>
</head>
<body>
Input: <input type="text" id="inputElement" onkeyup="myFunction(this.value)">
<script>
function myFunction(value) {
/* alert(`URL: http://127.0.0.1:5000/complete?station=${value}`); */
$.ajax({
type: "GET",
url: "http://127.0.0.1:5000/complete?station=" value,
success: function (data) {
if (data != '') {
$("#inputElement").autocomplete({
source: data
});
console.log(data)
}
},
/* I simulated the successful response from the server in the field below. */
error: function (data) {
$("#inputElement").autocomplete({
source: getList()
});
}
});
}
function getList() {
var availableTags = ["ActionScript", "AppleScript", "Asp", "BASIC", "C", "C ", "Clojure",
"COBOL", "ColdFusion", "Erlang", "Fortran", "Groovy", "Haskell", "Scala",
"Java", "JavaScript", "Lisp", "Perl", "PHP", "Python", "Ruby", "Scheme"
];
return availableTags;
}
</script>
</body>
</html>
uj5u.com熱心網友回復:
這樣做了:
$("#from").autocomplete({
source: function (request, response) {
$.ajax({
url: 'http://127.0.0.1:5000/complete?station=' $("#from").val(),
dataType: "json",
success: function (data) {
response(data);
},
error: function (xhr, status, error) {
alert("Error");
}
});
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/386013.html
標籤:javascript html 查询 阿贾克斯
