搜索自动完成ajax在laravel



我使用ajax进行实时搜索,但问题是它只显示一个结果

当我使用。html()时但当我使用append()时它工作但我写的每个单词都复制结果,

下面是我的代码: 在控制器中,

$patient = Patient::select('id', 'avatar')
->where('phone_number', 'like', '%' . $search_query . '%')
->orWhere('first_name', 'like', '%' . $search_query . '%')
->limit(15)
->get();
return $patient;

ajax代码在刀片

$("#search-eng").keyup(function() {
let search_query = $(this).val();
if (search_query != "") {
$.ajax({
url: '{{ url('/appointment/calander_patient_search') }}/' +
search_query,
type: "GET",
dataType: "json",
success: function(data) {
$("#search-eng-show-list").show();
if (data !== "") {
$.each(data, function(key, value) {
$('#search-eng-show-list').html(
'<a data-id="' + value.id + '"' value.second_name + '</a>');
});
}
if (data == "") {
$('#search-eng-show-list').html(
'<a><i "></i>No Record</a>'
);
}
},
});
} else {
$("#search-eng-show-list").empty();
$("#search-eng-show-list").hide();;
}
});

是的,你在循环语句中设置了内容,所以它只会取最后一个内容。

你可以使用一些缓冲区变量:

if (data !== "") {
var html = '';
$.each(data, function(key, value) {
html += '<a data-id="' + value.id + '"' value.second_name + '</a>');
}
$('#search-eng-show-list').html(html);
// ....

最新更新