Taking Dropdown’s Name and ID

Asked

Viewed 108 times

0

I have this Javascript that arrow the values to the Dropdown:

$('#Estado').change(function () {
var id = $(Estado).val();

    $(function buscarCidade() {
        //alert(id);
        var url = '@Url.Action("SelecaoCidade", "Chamada")';//url do controller que passará a informação
        $.post(url, { id: id },

             function (data) {//Caso o retorno dê certo
                 //window.alert(data.length);
                 $('#Cidade').children().remove();
                 if (data.length > 0) {
                     $.each(data, function (index, value) {
                         $('#Cidade').append('<option value="' + value.Id + '">' + value.Nome + '</option>');
                     });
                 } else {
                     var novaOpcao = $('<option value=""></option>');
                     $('#Cidade').append(newOption);
                 }//fim if else

             });//fim do post

    });//BuscarCidade

});//Estado

I need to get the name and ID of the cities that were entered, and I tried to use these code snippets to get the id, but it didn’t roll:

var idCid = City.value;

var idCid = $(City). val();

var idCid = City.index;

  • 1

    $("#Status"). attr("id"); See if you can do so.

  • 2

    I believe that simply by: var idCid = $('#Cidade').attr("id"); and `var name = $('#City'). attr("name");

  • Is there an error? What is the value of the "date" parameter? Is it really an object that contains the "Id" and "Name" properties? Make a console.log(data); and post the result here.

  • Douglasgarrido and Lucascosta attr("id") showed me only 'City' (not the name of the city), but attr("name") gave Undefined

  • @Filipemoraes I don’t know if it’s right, but I got this data: no element found abort:1:1 The getPreventDefault() method should no longer be used. Instead, use defaultPrevented. browserLink:37:40278 Referenceerror: data is not defined[Learn More] Vote:226:33

  • This is Json’s answer: [{"CidadeID":1,"Nome":"Aparecida","EstadoID":25,"Estado":null,"Locais":null},{"CidadeID":2,"Nome":"Guaratinguet&#xA;á","EstadoID":25,"Estado":null,"Locais":null},{"CidadeID":3,"Nome":"Roseira","EstadoID":25,"Estado":null&#xA;,"Locais":null},{"CidadeID":4,"Nome":"Lorena","EstadoID":25,"State":null,"Locations":null},{"Cidadeid" :5,"Name":"Taubaté","Estadoid":25,"State":null,"Locations":null},{"Cidadeid":6,"Name":"Caçapava","Estadoid" :25,"State":null,"Locations":null,"}]

  • So Fábio. The json does not have the property "Id" but "Cidadeid". In its code, where you have value.Id, change it to value.Cidadeid

  • Ball Show @Filipemoraes Valeu

Show 3 more comments

2 answers

1

To get the values of select you can use:

$("#cidade").change(function(){
  var idCid = $(this).attr("id")
    , valor = $(this).val()
    , nome = $(this).attr("name");
  console.log(idCid, nome, valor);  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="cidade" name="cidade">
  <option value="">Selecione</option>
  <option value="SP">São Paulo</option>
  <option value="CT">Curitiba</option>
</select>

  • by the example you gave me appears this: City Undefined Undefined

  • You probably shouldn’t have the attribute name and what is the value of value.Id when you make append in select?

  • from what I saw (Alert(value.Id)), is coming from Undefined

1


From what I read in the papers, json sent in response to a request POST does not own the property id and yes CidadeID, then in your code just replace where you have:

value.id

For:

value.CidadeID

Thus will be assigned the id correctly to each option.

Browser other questions tagged

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