jquery自动完成新的字段更新

时间:2018-01-29 09:46:36

标签: jquery autocomplete

我正在尝试将字段添加到数组中,并通过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"
            });
         }
      });
   });

1 个答案:

答案 0 :(得分:0)

我使用jquery自动填充选项找到了答案。

//something like this

$("#booker_name").autocomplete("option",{
  source:bookername
});