When closing modal, clear search result done in the bank

Asked

Viewed 3,488 times

2

I need to close and limparthe content of a modal. When opening it I have a search field where I perform a search in my bd, and by clicking the button enviar I close it by passing some parameters. But when calling the modal for a new search the previous search is still there. I have this code that sends the parameters and closes the modal:

<button type="button" class="btn btn-primary btn-mini" data-toggle="modal" data-dismiss="modal" onclick="mostraDados('parâmetros')"> enviar </button>

The called function is this:

function mostraDados(pIdCooperante,pNome,pIdPropriedade,pVistoria,pUF,pIdMunicipio){    

    // ATRIBUINDO VALORES RETORANDOS AOS CAMPOS
    $("#ID").val(pIdCooperante); 
    $("#Cooperante").val(pNome); 
    $("#Propriedade").val(pIdPropriedade);
    $("#Vistoria").val(pVistoria);
    $("#UF").val(pUF);
    $("#Municipio").val(pIdMunicipio);  

}

Had made some tests using the command $('#ModalCooperante').modal('hide'); but this only hides the modal and when I call again the previous research still remains.

  • Tries $('#ModalCooperante').dialog('destroy').remove()

  • Hello @Sorack, thanks for the tip, but it’s not working yet.

1 answer

6


You can set empty value for them one by one in the event, when you do the hide modal:

$("#ID").val(''); 
$("#Cooperante").val(''); 
$("#Propriedade").val('');
$("#Vistoria").val('');
$("#UF").val('');
$("#Municipio").val('');

Example:

$('button').on('click', function() {
  $('#form1 input').each(function() {
     $(this).val(''); 
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form id="form1">
  <input value="input1">
  <input value="input2">
  <input value="input3">
  <input value="input4">
  <input value="input5">
</form>

<button>Esconder Modal</button>

It seems to me that you are using bootstrap, if it is the default modal method of bootstrap, to 'grab' the event hide modal makes:

$('#myModal').on('hidden.bs.modal', function () {
  $('#form1 input').each(function() {
     $(this).val(''); 
  });
});

Browser other questions tagged

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