List by jquery and Submit action

Asked

Viewed 22 times

1

I have this script, that when selecting a state, it loads the municipalities that are working, but if I try to save by an action Submit it shows the error. But what the user chose in the municipality disappears, and municipalities are 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>
  • You can post a simulation to a JSFIDDLE or PLUNKER?

  • Reload the Page when it shows the right errors?

1 answer

0

It is because when you reload the Page, you need to give trigger in the field, for it to remake the logic that is in your change. Try to put this in your Script:

$(function(){
    $('#estadoMunicipio').trigger('change');
})

Browser other questions tagged

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