データベースを検索し、検索に関連するユーザーを返すオートコンプリート検索ボックスに取り組んでいます。これを行うには jquery ajax を使用しkeyup
ます。
$("#search").keyup(function(){
$(".itemSearch").remove();
$(".notfound").hide();
$("#searchResultSection").height("70px");
if(!$("#search").val()){
$("#searchResultSection").slideUp();
$(".ajaxLogoSearch").hide();
$(".notfound").hide();
}
else{
var data = {
query: $("#search").val()
};
$("#searchResultSection").slideDown();
$(".ajaxLogoSearch").show();
$.ajax({
url: '/ajax/search',
data: data,
dataType:'json',
success:function(result){
if(result.found==0){
$(".ajaxLogoSearch").hide();
$(".notfound").show();
}
else{
var searchResult = $("#searchResultSection");
$(".ajaxLogoSearch").hide();
searchResult.css({"height":20});
for(var i=0;i<result.users.length;i++){
var content = '<div class="itemSearch">\
<img src="../../static/social/images/defaultMaleImage.png" height="50px">\
<p>'+ result.users[i].firstname+ ' '+ result.users[i].lastname +'</p>\
</div>'
if(searchResult.height() < 370){
searchResult.height("+=70px");
}
$("#innerSearch").append(content);
}
$(".itemSearch").hover(function(){
$(this).css({"background":"rgb(55, 108, 221)"});
},function(){
$(this).css({"background":"#658be6"});
});
}
}
});
}
})
私の問題は、2 文字を入力してすぐに 2 つの ajax リクエストが送信され、検索結果ボックスに検索結果が 2 回表示されることです。
EDIT:あなたの答えに感謝します。 これはソリューションのリンクです