I have the following code:
$("#cep").on('mouseout', function() {
var url = "http://cep.republicavirtual.com.br/web_cep.php";
var cep = $(this).val();
$.ajax({
type: "GET",
dataType: 'json',
data: {'cep': cep, 'formato': 'json'},
async: false,
url: url,
success: function(response) {
if (response.resultado === '1') {
$("#bairro").val(response.bairro);
$("#endereco").val(response.logradouro);
var uf = response.uf;
$("select#estado option").each(function() {
this.selected = (this.text === uf);
});
$("#estado").trigger("change");
var cidade = response.cidade;
$("select#cidade option").each(function() {
this.selected = (this.text === cidade);
console.log(this.selected);
});
}
}
});
});
In this part of the code should be "set" the city in the combo that was returned via ajax. Note that this part comes after $("#estado").trigger("change")
;
var cidade = response.cidade;
$("select#cidade option").each(function() {
this.selected = (this.text === cidade);
});
It does not work, cities are populated, the city comes normally but is not set to default in the combo box.
How to solve this?