我正在尝试将字段添加到数组中,并通过ajax请求使用自动完成功能获取它。但是每次在自动完成时都没有更新值,直到页面刷新完成。
$("#booker_name").autocomplete({
source:bookername,
select: function( event, ui ) {
$(this).val(ui.item.label);
$( "#booker_phone" ).val( ui.item.booking_phoneno);
$( "#booker_email" ).val( ui.item.booking_email); //ui.item is your object from the array
return false;
}}).focus(function(){
console.log(bookername);
$.getJSON(url+companyId+'/', function(data) {
$.each(data,function(key,value){
bookername.push(value);
});
console.log(bookername);
});
}).focusout(function(){
bookername = [];
});
下面的ajax请求代码:
$.ajax({ //send json-object to api and read response
url: '/rapido/api/get_booker_list/',
method: "POST",
headers: {
'X-CSRFToken': '{{ csrf_token }}'
},
contentType: 'application/json',
data:JSON.stringify(booker),
success: function(res) {
swal({
title:"Booker details added successfully",
icon:"success"
});
}
});
});
答案 0 :(得分:0)
我使用jquery自动填充选项找到了答案。
//something like this
$("#booker_name").autocomplete("option",{
source:bookername
});