How to get the value of a UF list through the CEP query via Ajax?

2

I'm developing an address form where the user types the zip code and automatically fills in other fields via Ajax. The problem is that I can not get the UF value, which in my case is a list.

My example code:

  <fieldset>
<legend>Endereco:</legend>

    @Html.Label("Cep: ")
    @Html.TextBoxFor(e => e.CEP, new { maxlength = "9", id = "Cep", name = "Cep", })       
<br/> 

    @Html.Label("Endereco: ")
    @Html.TextBoxFor(e => e.DescricaoEndereco, new { maxlength = "50", id = "Endereco", name = "Endereco" })

    <br />
    @Html.Label("Número: ")
    @Html.TextBoxFor(e => e.Numero, new { maxlength = "50", id = "Numero" })
    <br />
    @Html.Label("Complemento: ")
    @Html.TextBoxFor(e => e.Complemento, new { maxlength = "50", id = "Complemento" })
    <br />
    @Html.Label("Logradouro: ")
    @Html.TextBoxFor(e => e.Logradouro, new { maxlength = "5", id = "Logradouro" })
    <br />

    @Html.Label("Bairro: ")
    @Html.TextBoxFor(e => e.Bairro, new { maxlength = "50", id = "Bairro", name = "Bairro" })
    <br />
    @Html.Label("Cidade: ")
    @Html.TextBoxFor(e => e.Cidade, new { maxlength = "40", id = "Cidade", name = "Cidade" })
    <br />
    @Html.Label("UF: ")
    @Html.DropDownListFor(e => e.UF, Model.UFList, new { id = "UF",name="UF" })

Javascript code:        

$(document).ready(function () {

    $("#zipcode").blur(function () {

        consulta = $("#zipcode").val()
        consulta = consulta.replace("-", "");

        var url = "http://cep.correiocontrol.com.br/" + consulta + ".json";

        $.ajax({
            url: url,
            type: 'GET',
            dataType: 'json',
            success: function (json) {
                $("#street").val(json.logradouro)
                $("#district").val(json.bairro)
                $("#city").val(json.localidade)
                $("UF").val(json.UF)
                switch (json.UF) {
                    case "AC":
                        $("#UF").val("Acre")
                        break;
                    case "AL":
                        $("#UF").val("Alagoas")
                        break;
                    case "AP":
                        $("#UF").val("Amapá")
                        break;
                    case "AM":
                        $("#UF").val("Amazonas")
                        break;
                    case "BA":
                        $("#UF").val("Bahia")
                        break;

                    case "CE":
                        $("#UF").val("Ceará")
                        break;
                    case "DF":
                        $("#UF").val("Distrito Federal")
                        break;
                    case "ES":
                        $("#UF").val("Espírito Santo")
                        break;
                    case "GO":
                        $("#UF").val("Goiás")
                        break;
                    case "MA":
                        $("#UF").val("Maranhão")
                        break
                    case "MT":
                        $("#UF").val("Mato Grosso")
                        break;
                    case "MS":
                        $("#UF").val("Mato Grosso do Sul")
                        break;
                    case "MG":
                        $("#UF").val("Minas Gerais")
                        break;
                    case "PA":
                        $("#UF").val("Pará")
                        break;
                    case "PB":
                        $("#UF").val("Paraíba")
                        break;
                    case "PR":
                        $("#UF").val("Paraná")
                        break;
                    case "PE":
                        $("#UF").val("Pernambuco")
                        break;
                    case "PI":
                        $("#UF").val("Piauí")
                        break;
                    case "RJ":
                        $("#UF").val("Rio de Janeiro")
                        break;
                    case "RN":
                        $("#UF").val("Rio Grande do Norte")
                        break;
                    case "RS":
                        $("#UF").val("Rio Grande do Sul")
                        break;
                    case "RO":
                        $("#UF").val("Rondônia")
                        break;
                    case "SC":
                        $("#UF").val("Santa Catarina")
                        break;
                    case "RR":
                        $("#UF").val("Roraima")
                        break;
                    case "SP":
                        $("#UF").val("São Paulo")
                        break;
                    case "SE":
                        $("#UF").val("Sergipe")
                        break;
                    case "TO":
                        $("#UF").val("Tocantins")
                        break;

                    default:
                        $("#UF").val("")
                }

            },
        });

    });
});

    
asked by anonymous 22.01.2015 / 12:52

1 answer

1

Partner, since your state field is a list, you can not use .val (), you will need to SELECT an item from your list. Try the following:

Switch your entire switch case over the line below:

$("#UF option[value='"+json.UF+"']").attr("selected", true);

- I do not particularly like the $ .ajax (), I'd rather use the:

$.getJSON( "url.php", function(data){});

With this I can handle the return. But you can use the $ .ajax () .done (), .error (), and .fail () methods. So if you do not fall into success, come pro error or fail.

    
22.01.2015 / 13:14