Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
How to get async in jQuery autocomplete

I am using the jQuery autocomplete plugin. The situation is that the hint data is taken via the GET API method. I can't make the code wait until the end of accepting data from the API.

$('#vehicleBrand').autocomplete({
  source: function(request, response) {
    $('#vehicleBrandValue').val('');
    $.get('api/brands', {
      query: request.term,
      category: $('select[name="vehicleType"]').val()
    }, function(data) {
      response(data)
    })
  },
  select: function(event, ui) {
    $('#vehicleBrandValue').val(ui.item.data);
    event.target.classList.remove('border-danger');
    prepareModels(ui.item.data);
  },
  close: function(event, ui) {
    if (!$('#vehicleBrandValue').val()) {
      alertify.error('Error text here!');
      event.target.value = '';
      event.target.classList.add('border-danger');
      event.target.focus();
    }
  }
});

In the code above, on the source key, I call the get method to get the data.

let brand = data.car_brand;
$('#vehicleBrand').data("ui-autocomplete").search(brand) 
$("#vehicleBrand").data("ui-autocomplete").menu.element[0].firstChild.click()

In the code above I am looking for a car brand and click on it through the script. But the click occurs before the request for data on the source key has time to complete, and the click occurs in the undefined list. Where can I make the click wait for a response from the function by the key source ?

I tried to use Promise and async/await in function by key source, but it didn't work

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Might advise the following:

$('#vehicleBrand').autocomplete({
  source: function(request, response){
    $.getJSON('api/brands', {
      query: request.term,
      category: $('select[name="vehicleType"]').val()
    }, function(data){
      response(data);
    });
  },
  select: function(event, ui) {
    $('#vehicleBrandValue').val(ui.item.value);
    $(this).removeClass('border-danger');
    prepareModels(ui.item.label);
  },
  close: function(event, ui) {
    if (!$('#vehicleBrandValue').val()) {
      alertify.error('Error text here!');
      $(this).val("").addClass('border-danger').focus();
    }
  }
});

Need to also make sure that you have { label, value } pair someplace in your data results.

See more: https://api.jqueryui.com/autocomplete/#option-source

Using $.getJSON() is short hand for a AJAX GET Request that is expecting JSON data as a result.

If you are later using this code:

let brand = data.car_brand;
$('#vehicleBrand').data("ui-autocomplete").search(brand) 
$("#vehicleBrand").data("ui-autocomplete").menu.element[0].firstChild.click()

The code will execute right away, before results have loaded. You may need to delay the trigger using setTimeout since there is not a Promise to execute after. Something like this might work:

$('#vehicleBrand').autocomplete("search", data.car_brand);
setTimeout(function(){
  $("#vehicleBrand").autocomplete("widget").find(".ui-menu-item:eq(0)").trigger("click");
}, 1200);

This give Autocomplete time to load the events before triggering a click event.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda