Close Bootstrap modal on form submit

后端 未结 13 1436
盖世英雄少女心
盖世英雄少女心 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:21

    Add the same attribute as you have on the Close button:

    data-dismiss="modal"
    

    e.g.

    <button type="submit" class="btn btn-success" data-dismiss="modal"><i class="glyphicon glyphicon-ok"></i> Save</button>
    

    You can also use jQuery if you wish:

    $('#frmStudent').submit(function() {
    
        // submission stuff
    
        $('#StudentModal').modal('hide');
        return false;
    });
    
    0 讨论(0)
  • 2020-12-08 07:22

    give id to submit button

    <button id="btnSave" type="submit" class="btn btn-success"><i class="glyphicon glyphicon-trash"></i> Save</button>
    
    $('#btnSave').click(function() {
       $('#StudentModal').modal('hide');
    });
    

    Also you forgot to close last div.

    </div>
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-08 07:22

    if your modal has a cancel button (Otherwise create a hidden close button). You can simulate the click event on this button so that your form is closed. Iin your component add a ViewChild

    export class HelloComponent implements OnInit {
    
    @ViewChild('fileInput') fileInput:ElementRef;
    

    in your close button add #fileInput

    <button class="btn btn-danger" #fileInput id="delete-node" name="button" data-dismiss="modal" type="submit">Cancelar</button>
    

    When you want to close the modal programmatically trigger an click event on the close button

    this.fileInput.nativeElement.click();
    
    0 讨论(0)
  • 2020-12-08 07:23

    I had the same problem and finally got it working with this code:

    <%=form_with id: :friend_email_form, url: friend_emails_create_path do |f|%>
    
                          # form fields entered here
    
    <div class="actions">
        <%= f.submit "Send Email", class: 'btn btn-primary', "onclick":"submit_form();", "data-dismiss":"modal"%>
    </div>
    
     <script>
        function submit_form() {
          document.getElementById('friend_email_form').submit();
        }
    </script>
    

    The selected answer did not work for me.

    0 讨论(0)
  • 2020-12-08 07:26

    i make this code it work fine but once form submit model button not open model again say e.preventdefault is not a function..

    Use below code on any event that fire on submit form

                    $('.modal').removeClass('in');
                    $('.modal').attr("aria-hidden","true");
                    $('.modal').css("display", "none");
                    $('.modal-backdrop').remove();
                    $('body').removeClass('modal-open');
    

    you can make this code more short..

    if any body found solution for e.preventdefault let us know

    0 讨论(0)
  • 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.

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