multipleSelect jQuery does not update the received values in JSON dynamically

1

Can you explain to me why the jQuery multipleSelect () class does not update the received values via JSON ?

For example, when I select a state, in my other selection field, the related cities of that state are filtered. When I linko the jQuery in the cities field, the field is only blank, that is, it only receives the value of when the page is loaded, which in this case is in white.

I want to know how I retrieve the state and play the dynamically filtered cities, and where do I link to my cities field?

$('select[name=uf]').on('change', function () {
    var uf = $(this).val();
    $.get('/judicial/get-cidades/' + uf, function (busca) {
        $('select[id=comarca_id]').empty();
            $('select[id=comarca_id]').append('<option value=""> </option>');
        $.each(busca, function (key, value) {
            $('select[id=comarca_id]').append('<option value=' + value.name + '>' + value.name + '</option>');
        });
    });
});
$(document).ready(function(){
    $('select[name=comarca_id]').multiselect({
        numberDisplayed: 0,
        includeSelectAllOption: true,
        allSelectedText: 'Todos',
        nonSelectedText: 'Selecione',
        nSelectedText: 'Selecionado',
        selectAllText: 'Todos',
    });
});

Here is the link where I got this jQuery .

    
asked by anonymous 02.05.2017 / 17:12

1 answer

1

"I solved the problem". I solved the problem by leaving it in a way that I could use in production.

$('select[name=uf]').on('change', function () { // ativa quando selecionar uma UF
  $('select[name=comarca_id]').multiselect('destroy'); // destroy o multiple select existente
  var uf = $(this).val();
  $.get('/judicial/get-cidades/' + uf, function (busca) { // buscando as cidades pela rota get-cidades + uf
      $('select[id=comarca_id]').empty(); // esvaziando os options das comarcas
      $.each(busca, function (key, value) {
          $('select[id=comarca_id]').append('<option value=' + value.name + '>' + value.name + '</option>'); // inserindo os options com as cidades
      });
    $('select[name=comarca_id]').multiselect(); // ativando o multiselect no campo após fazer todo o carregamento das cidades
  });  });
    
03.05.2017 / 16:35