JSON assigned via JS to a <select>

Asked

Viewed 66 times

0

Good afternoon. I use a JSON response search for a UF and City field. To recover 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 correctly. The select that receives the found cities is multselection, so to not 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 extension of jquery, I no longer get the values in the cities field, the field is left with no option. I will place 2 images demonstrating what happens.

Without the jquery. inserir a descrição da imagem aqui

With jquery.

inserir a descrição da imagem aqui

  • Uncaught Typeerror: $(...). appendchild is not a Function. This is happening in the log

  • what the documentation page of this extension is using?

  • https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js

  • There’s one more script tbm, but I don’t have the way.

  • In case that’s the main thing about your problem...

No answers

Browser other questions tagged

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