JSON assigned via JS to a select

0

Good afternoon. I use a search with response in JSON for a UF field and City. To recover the cities by UF with JSON, I use this code in JS.

$('select[id=uf]').change(function () { // ativa a função quando é selecionado uma UF pelo id = uf
var uf = $(this).val(); // recebe o valor da UF

$.get('/get-cidades', {uf : uf}, function (busca) { // pesquisa pela url com a rota /get-cidades/uf-selecionada
    $('select[name=cidades]').empty(); // procura o campo com o name = cidades
    $.each(busca, function (key, value) {
        $('select[name=cidades]').append('<option value=' + value.id + '>' + value.name + '</option>'); // add os option
    });
});});

So far so good, everything works properly. The select that receives the found cities is of multiselection, so not to leave the field in the original HTML style, I use a jquery extension.

$('#cidades').multiselect({
numberDisplayed: 0,
includeSelectAllOption: true, });

The problem is that when I add this jquery extension, I no longer receive the values in the cities field, the field is left with no option. I will put 2 images showing what happens.

Without jquery.

Withjquery.

    
asked by anonymous 24.04.2017 / 20:15

0 answers