【发布时间】:2023-03-29 09:50:01
【问题描述】:
我目前正在完成作为网络开发人员培训的最终团队项目。我确实为我们的应用程序编写了一个实时搜索代码,它按预期工作,直到我从输入中删除内容为止。代码有时会保留在 if 语句中以删除附加。我不明白为什么。有人知道吗?
这是我的代码:
$(function(){
var check = 0;
$("#getQuestions").on("keyup", function(){
check++;
var inputLength = $("#getQuestions").val().length;
if (check === 3 || inputLength === 0) {
$(".liveSearch").remove();
}
if (inputLength >= 1) {
if(check === 3 ){
$.ajax({
type: "POST",
url: "/getQuestions",
data: {"question": $("#getQuestions").val()}
}).done(function(response){
for (var i = 0; i < response.length; i++) {
var id = response[i].id;
var title = response[i].title;
$("#listQuestions").append('<div class="liveSearch"><a href="/question?id='+id +'""' + '>' + title + '</a></div>' );
}
check = 0;
});
}
}
});
});
感谢您的帮助。
祝你有美好的一天。
【问题讨论】:
-
您能出示您的完整代码吗?如果可能的话,创建一个小提琴。
标签: jquery livesearch