autocomplete does not work after calling ajax

Asked

Viewed 72 times

0

I have a zip code field with autocomplete that works perfectly when I type something. However this field is also filled after an ajax call that fills the zip code automatically, only when receiving the (full) value of the call the autocomplete does not work.

Part of my autocomplete:

$('#PostalCode').autocomplete({
minLength: 9,
source: function(){
            var value = document.getElementById('PostalCode').value;
            var nameAction =  @Html.Raw(Json.Encode(ViewBag.ActionName));
            if (value !== null && value !== "") {
                $.ajax({
                    method: "GET",
                    url: "@Url.Action("getFullAddress", "DeliveryService")",
                    data: { postalCode: value, nameAction: nameAction },
                    dataType: 'json',
                    success: function (data) {
                        $('#AddressStreet').val(data.AddressStreet);
                        $('#AddressNeighborhood').val(data.AddressNeighborhood);
                        $('#AddressCity').val(data.AddressCity);
                        $('#IdState').val(data.IdState);
                        $('#Latitude').val(data.Latitude);
                        $('#Longitude').val(data.Longitude);
                    },
                    error: function () {
                        $('#AddressStreet').val("");
                        $('#AddressNeighborhood').val("");
                        $('#AddressCity').val("");
                        $('#AddressNumber').val("");
                        $('#AddressComplement').val("");
                        $('#IdState').val(""); 
                    }
                });
            }
        }

The field receiving the value after the call:

$('#PostalCode').val(result.PostalCode);

I wanted to fire the autocomplete when the field receives the value of the AJAX call.

UPDATE

I put the code that makes the search for the zip code inside a Function getCEP(), I can call it after the AJAX call and inside the autocomplete, and take the value of the in the field using the getElementbyId.

  • Are you using the source: of autocomplete? you can put the ajax code?

  • I edited the question with the rest of the code of the autocomplete, there works well, just does not fire the autocomplete when the field postalCode receives the complete zip code of another call

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.