我正在尝试在实现自动完成输入中显示一些搜索结果
但它不能正常工作
所以当我写一些像“ Galaxy S9”的词时
仅显示一个结果:https://imgur.com/a/EOb7B9R
但是当我在控制台中打印结果时,它显示了以下内容:https://imgur.com/M87x4Hm
同样,将鼠标移出并重新单击搜索栏时,它也会显示所有结果:https://imgur.com/QccTmGm
这是我的代码:
html:
<form autocomplete="off" class=" hide-on-med-and-down" id="form1">
<div class="input-field" style="max-width: 300pt;">
<input class="autocomplete" id="autocomplete-input" type="search">
<label class="label-icon" for="autocomplete-input">
<i class="material-icons prefix searchbar">search</i></label>
<i class="material-icons rtlx">close</i>
</div>
</form>
javascript :
var elems = document.querySelectorAll('#autocomplete-input');
var instances = M.Autocomplete.init(elems, {
limit: 4,
});
$('#autocomplete-input').keyup(function () {
console.log($('#autocomplete-input').val());
$.ajax({
type: "GET",
url: "https://localhost:44358/searching?text=" + $('#autocomplete-input').val(),
processData: false,
contentType: "application/json",
success: function (data) {
var response = data;
let dataresp = [];
for (var i = 0; i < response.length; i++) {
dataresp[response[i].brand + ' ' + response[i].model] = response[i].imgUrl;
}
try {
console.log(dataresp);
instances[0].updateData(dataresp);
} catch (e) {
console.log(e);
}
}
});
});
Github中提到的相同问题: https://github.com/Dogfalo/materialize/issues/5690