I have a zip field with autocomplete
that works perfectly when I type something. However, this field is also populated after an ajax call that fills in the zip code automatically, but when it receives 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 autocomplete
when the field receives the value of the AJAX call.
UPDATE
I put the code that searches the zip code inside a getCEP()
function, so I can call it after the AJAX call and inside the autoComplete, and get the value of the in the field using getElementbyId
.