我正在为一个项目建立一个小网站,我一直在搜索如何使用Materialise插件填充我的自动完成输入。我对json或ajax不是很熟悉,所以我真的很痛苦。 doc中的原始示例与静态数据一样:
$('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);
?>
我假设代码正在运行,因为它显示[&#34; JAVA&#34;:null,&#34; js&#34;:null,&#34; C&#34;:null ]匹配我的数据库数据。 知道如何将这个json数据放在这个数据参数中而不是静态名称吗?
data: {
"Apple": null,
"Microsoft": null,
"Google": 'http://placehold.it/250x250'
}
感谢您的时间!
答案 0 :(得分:0)
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.
});
});
&#13;