0

HTML:

<input type="submit" class="general-btn" value="Submit" name="decision" id="trigger">

First JS call:

$("#trigger").submit(function (e) {
    userSubmit(e);
});

Then I followed this fiddle from jQuery Deferred and Dialog box

function userSubmit(e){
var question = "Do you want to start a war?";
    confirmation(question).then(function (answer) {
        console.log(answer);
        var ansbool = (String(answer) == "true");
        if(ansbool){
            alert("this is obviously " + ansbool);//TRUE
        } else {
            alert("and then there is " + ansbool);//FALSE
        }
    });
}

but the form is submitting before the confirmation is answered.

woninana
  • 3,409
  • 9
  • 42
  • 66

2 Answers2

0

The confirmation is asynchronous, so the submit handler does not block and returns immediately, causing the submit.

You can stop the submit using e.preventDefault() and then trigger the submit manually using the submit function. However you need to keep a flag that checks whether you already confirmed, so you do not cancel and ask again, or you can remove the submit handler if the submit was confirmed.

H.B.
  • 166,899
  • 29
  • 327
  • 400
-2

Instead of using the code you used, I usually with a form use the onclick attribute instead for the 'HTMLInputElement.onclick' as the console would say.

<input type="submit" onclick="myFunction()" />
L. O. L.
  • 1
  • 3