When closing modal, clear search result done in the bank

2

I need to close and limpar the contents of a modal. When I open it I have a search field where I perform a search in my bd , and when I click on the enviar button 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);  

}

I had done some tests using the command $('#ModalCooperante').modal('hide'); but this only hides the modal and when I call it again the previous search still remains.

    
asked by anonymous 30.11.2016 / 17:25

1 answer

5

You can set empty value for them one by one in the event, when you do hide of the 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><formid="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 bootstrap default modal method, to 'grab' the event hide of the modal does:

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