Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close bootstrap modal after submit

How can I close the bootstrap modal after I click on the Delete button? Here's my code:

<div id="media_delete_confirmation" class="modal fade">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title">Confirmation</h4>
      </div>

      <form id="modal-form">
        <div class="modal-body">
          <input id="media_action" value="deleteMediaAction" type="hidden"/>
          <p>Do you want to save changes you made to document before closing?</p>
          <p class="text-warning"><small>If you don't save, your changes will be lost.</small></p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-primary" data-dismiss="modal">Keep</button>
          <button type="button" class="btn btn-default" id="modal_delete">Delete</button>
        </div>
      </form>
    </div>
  </div>
</div>

and here's the other part:

$("#modal_delete").click(function() {

  var id = $(".image-picker").val();
  var media_action = $("#media_action").val();

  $.ajax({
    type: 'POST',
    url: '?page=myMediaController&action=deleteMedia',
    data: {'media_id' : id},
    success: function(data) {
      $("#media_delete_confirmation").modal("hide");
    }
  });
});
like image 936
asdasdas Avatar asked Apr 20 '15 17:04

asdasdas


2 Answers

I had the same issue, and was able to get the modal dialog to close this way:

<div id="approveDialog" class="modal approveDialog hide">
    <!-- various form elements -->
    <div class="modal-footer pull-center">
            <a href="#" data-dismiss="modal" aria-hidden="true" class="btn">Back to Listings</a>    
            <button id="submitApprove" class="btn btn-success">Shop</button>
    </div>
</div>


$('button#submitApprove').on("click", function(event) {
    // submit form via ajax, then

    event.preventDefault();
    $('#approveDialog').modal( 'hide' );
});
like image 110
yetimoner Avatar answered Oct 09 '22 18:10

yetimoner


No need to hide model using javascript, you can simply use data-dismiss="modal" in the button tag attribute as follows.

<div class="modal-footer">
  <button type="button" class="btn btn-primary" data-dismiss="modal">Keep</button>
  <button type="button" class="btn btn-default" id="modal_delete"  data-dismiss="modal">Delete</button>
</div>
like image 23
Nilesh Mahajan Avatar answered Oct 09 '22 20:10

Nilesh Mahajan