我正在为一个项目构建一个小网站,并且我已经花了几个小时搜索如何使用 Materialise 插件填充我的自动完成输入。我对 json 或 ajax 不太熟悉,所以我真的很痛苦。文档中的原始示例与静态数据类似:
$('input.autocomplete').autocomplete({
data: {
"Apple": null,
"Microsoft": null,
"Google": 'http://placehold.it/250x250'
},
limit: 20, // The max amount of results that can be shown at once. Default: Infinity.
onAutocomplete: function(val) {
// Callback function when value is autcompleted.
},
minLength: 1, // The minimum length of the input for the autocomplete to start. Default: 1.
});
我的愿望是从我的数据库中获取动态数据。 我正在使用这个 php 代码来执行此操作:
<?php
$query = $arg;
echo $query;
$json_output = array();
$reponse = $bdd->query("SELECT CPnom FROM competence where CPnom LIKE ". $query);
while ($donnees = $reponse->fetch()) {
$json_output[] = $donnees[0]. ": null";
}
return json_encode($json_output);
?>
我假设代码正在工作,因为它显示与我的数据库数据匹配的 ["JAVA":null, "js":null, "C":null] 。 知道如何将此 json 数据而不是静态名称放入此数据参数中吗?
data: {
"Apple": null,
"Microsoft": null,
"Google": 'http://placehold.it/250x250'
}
感谢您的宝贵时间!
最佳答案
var data_2 = JSON.parse(sessionStorage.getItem('key_1'));
$(function() {
$('input.autocomplete').autocomplete({
data: data_2,
limit: 20, // The max amount of results that can be shown at once. Default: Infinity.
onAutocomplete: function(val) {
// Callback function when value is autcompleted.
},
minLength: 2 // The minimum length of the input for the autocomplete to start. Default: 1.
});
});
关于javascript - 使用 json 数据填充 Materialise 自动完成功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43256002/