How do I close a modal window after AJAX success

前端 未结 6 1017
梦如初夏
梦如初夏 2020-12-17 14:19

I have a button which opens a modal but I have prevented the modal close by clicking background or ESC key.

My button looks like this:

相关标签:
6条回答
  • 2020-12-17 15:05

    To close bootstrap modal you can pass 'hide' as option to modal method as follows.

    $('#CompanyProfile').modal('hide');
    

    Use this code inside ajax success.

    Fiddle Demo

    0 讨论(0)
  • 2020-12-17 15:09

    You can try something like below. Untested but you'll get the idea.

    $.ajax({
      url: 'yourscript.php',
      data: $('form#yourForm').serialize(),
      type: 'post'
    }).done(function(response) { 
      // trigger modal closing here since ajax is complete.
    });
    
    0 讨论(0)
  • 2020-12-17 15:09

    I define my modal :

      <div class="modal fade" aria-hidden="true" role="dialog" id="modal" tabindex="-1">
        <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button id="btnCloseModal" hidden type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <strong>Waiting</strong>
                </div>
                <div class="modal-content">
                    <div>
                        Please don't close your tab!
                    </div>
                    <div class="d-flex justify-content-center">
                        <div class="spinner-border" role="status">
                            <span class="sr-only">Loading...</span>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <strong>Loading...</strong>
                </div>
            </div>
        </div>
    </div>
    

    then I use create function :

    var StopLoadingAnimation = function () {
    $('#modal').on('show.bs.modal', function (e) {
        console.log("trigger show");
        $("#btnCloseModal").trigger("click");
    });
    $('#modal').on('shown.bs.modal', function (e) {
        console.log("trigger");
        $("#btnCloseModal").trigger("click");
    });
    $('#modal').on('hidden.bs.modal', function (e) {
        console.log("event");
        $(e.currentTarget).off('shown');
        $(e.currentTarget).off('show');
    });
    $("#btnCloseModal").trigger("click");
    

    }

    My idea is after ajax success is will call function StopLoadingAnimation what will trigger event click on element btnCloseModal ( It like you click button btnCloseModal when you closing modal )

    0 讨论(0)
  • 2020-12-17 15:10
    success :function(){
                  console.log("success");
                  $('#contact_modal').html("<img src='img/bg/success.gif'>").delay(3000).fadeOut(450);
                  $('body').removeClass('modal-open');
                  $('.modal-backdrop.show').css('opacity','0');
                  $('.modal-backdrop').css('z-index','-1')
              }
    
    0 讨论(0)
  • 2020-12-17 15:12

    You can try something like

     $( document ).ready(function() {               
            $(".btn").button().click(function(){         // button click
    
                    $('#CompanyProfile').modal('show')   // Modal launch
                         $.ajax({
                                  type: "POST",
                                  url: "url",
                                  data:{data:data},
                                  cache: false,                         
                                  success: function(data) { 
                                                      $('.text').html(data); 
                                                     }
                       })                          
             });   
      });
    

    Make sure the modal is launched only once

    0 讨论(0)
  • 2020-12-17 15:19

    Adding this solution since there doesn't seem to be a generic method listed.

    If you wish to close any modal on success, but don't wish to hand-craft each $.ajax call to the server, you could use the following :

    $('.modal form').on('submit', function(event) {
        event.preventDefault();
        $.ajax({
            url: $(this).attr('action'),
            type: 'POST',
            data: $(this).serializeObject(),
            contentType: 'application/json',
            beforeSend: function(xhr){xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded")},
            success: function(data) {
                console.log(data.success);
                if(data.success===1) {
                    // loop through all modal's and call the Bootstrap
                    // modal jQuery extension's 'hide' method
                    $('.modal').each(function(){
                        $(this).modal('hide');
                    });
                    console.log('success');
                } else {
                    console.log('failure');
                }
            }
        });
    });
    

    As an aside, if you are looking to use the code above as copy/paste, you will need the following which takes form data and converts it into JSON for posting to the server, or change the $(this).serializeObject() to $(this).serialize() :

    $.fn.serializeObject = function() {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function() {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        o = { request: o };
        return o;
    };
    

    Reference: Bootstrap JS Modal

    0 讨论(0)
提交回复
热议问题