oli*_*gan 2 ajax jquery autocomplete
我正在尝试对某些国家/地区进行 jquery ui 自动完成,我通过 ajax 调用从数据库获得
我正在努力研究如何将我的值表传递给自动完成
$( document ).ready(function() {
$.ajax({
url:Routing.generate('e_veilleur_user_register_countries_autocomplete'),
type:"get",
dataType: 'json',
data: 'test=cool',
async: true,
cache: true,
success: function(data){
var availableTags = data;
}
});
$( "#fos_user_registration_form_pays" ).autocomplete({
source: availableTags
});
});
Run Code Online (Sandbox Code Playgroud)
我的ajax调用的结果是
[{"countryName":"United States"},
{"countryName":"Canada"},
{"countryName":"Afghanistan"},
{"countryName":"Albania"},
{"countryName":"Algeria"}
Run Code Online (Sandbox Code Playgroud)
给出的错误: availableTags is not defined
您可以为源使用自定义函数,该函数使用 AJAX。因此,您不必与范围外的 AJAX 调用同步。
$( "#fos_user_registration_form_pays" ).autocomplete({
source: function(request, response) {
$.ajax({
url:Routing.generate('e_veilleur_user_register_countries_autocomplete'),
type:"get",
dataType: 'json',
data: 'test=cool',
async: true,
cache: true,
success: function(data){
response(data);
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
编辑
直到现在,我才看到评论。回答该特定评论,您只需从 AJAX 中调用自动完成即可。
$.ajax({
url:Routing.generate('e_veilleur_user_register_countries_autocomplete'),
type:"get",
dataType: 'json',
data: 'test=cool',
async: true,
cache: true,
success: function(data){
$( "#fos_user_registration_form_pays" ).autocomplete({
source: data
});
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2085 次 |
| 最近记录: |