Close Bootstrap modal on form submit

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

    Use that Code

     $('#button').submit(function(e) {
        e.preventDefault();
        // Coding
        $('#IDModal').modal('toggle'); //or  $('#IDModal').modal('hide');
        return false;
    });
    
    0 讨论(0)
  • 2020-12-08 07:31

    If you do not want to use jQuery, make the button type a normal button and add a click listener pointing to the function you would like to execute, and send the form in as a parameter. The button would be as follows:

    <button (click)="yourSubmitFunction(yourForm)" [disabled]="!yourForm.valid" type="button" class="btn btn-primary" data-dismiss="modal">Save changes</button>

    Remember to remove: (ngSubmit)="yourSubmitFunction(yourForm)" from the form div if you use this method.

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

    I am using rails and ajax too and I had the same problem. just run this code

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

    that worked for me but I am trying to also fix it without using jQuery.

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

    Use this to submit and close the modal at the same time

    $('#form-submit').on('click', function(e){
        e.preventDefault();
        $('#con-close-modal').modal('toggle'); //or  $('#IDModal').modal('hide');
        $('#date-form').submit();
    });
    
    0 讨论(0)
  • 2020-12-08 07:39

    Neither adding data-dismiss="modal" nor using $("#modal").modal("hide") in javascript worked for me. Sure they closed the modal but the ajax request is not sent either.

    Instead, I rendered the partial containing the modals again after ajax is successful (I'm using ruby on rails). I also had to remove "modal-open" class from the body tag too. This basically resets the modals. I think something similar, with a callback upon successful ajax request to remove and add back the modals should work in other framework too.

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

    You can use one of this two options:

    1) Add data-dismiss to the submit button i.e.

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

    2) Do it in JS like

    $('#frmStudent').submit(function() {
        $('#StudentModal').modal('hide');
    });
    
    0 讨论(0)
提交回复
热议问题