Destroy modal bootstrap 3

2

I have not found a way to destroy the modal, currently I only hide them, which means that I have to manually clean the fields of the modal, I have tried the following without results:

$('#Modal').modal('hide');
$('#Modal').removeData();
$('#Modal').data('modal', null);

The .remove () closes the modal but, causes the screen to go black and does not allow me to take any action.

$('#Modal').remove();//provoca un bug

Also I'm using jquery 2.02 greetings thank you!

    
asked by Javier Antonio Aguayo Aguilar 17.05.2017 в 15:35
source

1 answer

5

The manners according to the documentation can not be destroyed because they do not have a method for it.

Since you are mentioning about cleaning the fields, here it is best to reset the form with:

$('#idForm')[0].reset()

or:

$('#miFormulario').trigger("reset");

I'll give you an example:

//Limpiando form al ocultar el modal
$('.bs-example-modal-sm').on('hidden.bs.modal', function(e) {
  $('#foma1')[0].reset()
})

//Limpiando form usando un boton
$('#limpio').on('click', function() {
  $('#miFormulario').trigger("reset");
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<!-- Small modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bs-example-modal-sm">Small modal</button>

<div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel">
  <div class="modal-dialog modal-sm" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">Modal title</h4>
      </div>
      <div class="modal-body">

        <form id="foma1">
          <input type="text" />
        </form>
        
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button id='limpio' type="button" class="btn btn-primary">Limpiar</button>
      </div>
    </div>
  </div>
</div>
    
answered by 17.05.2017 / 16:48
source