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
Use that Code
$('#button').submit(function(e) {
e.preventDefault();
// Coding
$('#IDModal').modal('toggle'); //or $('#IDModal').modal('hide');
return false;
});
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.
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.
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();
});
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.
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');
});