Close Bootstrap modal on form submit

后端 未结 13 1488
盖世英雄少女心
盖世英雄少女心 2020-12-08 07:08

I have a Bootstrap modal dialog which contains a form. The modal dialog contains a submit and a cancel button. Now on submit button click the form is submitted

13条回答
  •  无人及你
    2020-12-08 07:28

    The listed answers won't work if the results of the AJAX form submit affects the HTML outside the scope of the modal before the modal finishes closing. In my case, the result was a grayed out (fade) screen where I could see the page update, but could not interact with any of the page elements.

    My solution:

    $(document).on("click", "#send-email-submit-button", function(e){
        $('#send-new-email-modal').modal('hide')
    });
    
     $(document).on("submit", "#send-email-form", function(e){
        e.preventDefault();
        var querystring = $(this).serialize();
        $.ajax({
            type: "POST",
            url: "sendEmailMessage",
            data : querystring,
            success : function(response) {
                $('#send-new-email-modal').on('hidden.bs.modal', function () {
                    $('#contacts-render-target').html(response);
                }
        });
        return false;
    });
    

    Note: My modal form was inside a parent div that was already rendered via AJAX, thus the need to anchor the event listener to document.

提交回复
热议问题