3
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 "set" the city in the combo that was returned via ajax. Note that this part comes after the $("#estado").trigger("change");
var cidade = response.cidade;
$("select#cidade option").each(function() {
   this.selected = (this.text === cidade);
});
Does not work, cities are filled, the city comes normally but is not "set" as default in combo box.
How to solve this?
You can post here the
console.log(response)? it seems to me he’s returning xml.– Sergio
Your options have no value?
– bfavaretto
yes, it has value but the return comes as text and so I have to use so
– csassis