multipleSelect jQuery does not update incoming JSON values dynamically

Asked

Viewed 45 times

1

Could someone explain to me why the class multipleSelect() of jQuery does not update the values received 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 field of cities, the field is only blank, that is, it only receives the value of when the page is loaded, which in the case is blank.

I want to know how I recover the state and play the cities filtered dynamically, and where I connect to my field cities?

$('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 from where I got this jQuery.

  • Put the code in so people can take a look...

  • I put js where I connect jquery to the city field

  • You have tried running $('select[name=comarca_id]'). multiselect({... again after you have finished filling select?

  • First I play the cities filtered by the state in select and then I run multiselect(). I put in on my question now how I play the dice in select tbm..

1 answer

1

"I solved the problem". I solved the problem leaving in a way that I can 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
  });  });

Browser other questions tagged

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