Saturday, 3 December 2016

Confirming a form submission with a custom model popup

I have some delete forms in my app that I want to confirm with javascript/jQuery before submission.
The easy way is to do this:
$('form.confirm-form').submit(function(){
    return confirm('Are you sure?');
});
Which brings up a borwser-native confirmation box and only submits the form if the user clicks OK.
For something prettier I thought I use a Bootstrap modal for the confirm and checked out the Bootbox.js library for this.
I can't seem to get it working properly as it requires a callback and does not prevent the form from submitting. So I use preventDefault to stop the form submitting prematurely but now
  1. I can't dismiss the modal with the cancel button, and
  2. the OK button fails to submit the form.
Here's my code:
$('form.confirm-form').submit(function(event) {
    event.preventDefault();
    bootbox.confirm("Are you sure?", function(result){
        return result;
    });
});
How can I replicate the behaviour of the native confirm with this library?

--------------------------------------------------------------------------------------------------------------------------


Answer;



Try this FOUND HERE
$('form.confirm-form').submit(function(event) {

    var currentForm = this;
    event.preventDefault();
    bootbox.confirm("Are you sure?", function(result){
        if(result)
         {
   currentForm.submit()
         }
    });
});













No comments:

Post a Comment