List by jquery and submit action

1

I have this script, which when selecting a state, it loads the counties that are working, but if I try to save by a submit action it shows the error. But what the user chose in the municipality goes away, and the municipalities go blank.

<script type="text/javascript">
  $(function() {
    $('#estadoMunicipio')
    .change(
      function() {
        if ($(this).val()) {
          $('#municipioEstado').hide();
          $('.carregando').show();
          $.getJSON('/nota-fiscal-servico-web/buscaMunicipioPorPaisEstado/' + $(this).val(),
            function(j) {
              var options = '<option value="" class="chosen-select">'
              +'</option>';
              for (var i = 0; i < j.length; i++) {
                options += '<option class="chosen-select" value="' +
                j[i].id + '">'
                + j[i].descricao
                + '</option>';
              }
              $('#municipioEstado')
              .html(options)
              .show();
              $('.carregando').hide();
            });
        } else {
          $('#municipioEstado')
          .html(
            '<option  class="chosen-select" value="">-- Escolha um estado --</option>');
        }
      });
  });
</script>
    
asked by anonymous 17.01.2018 / 04:14

1 answer

0

It's because when you reload the Page, you need to give trigger in the field, so that it redo the logic that is in your change event. Try to put this in your Script:

$(function(){
    $('#estadoMunicipio').trigger('change');
})
    
17.01.2018 / 11:42