Submit or cancel submission form after confirmation

Can I cancel the submit form if I click Cancel?

$("#button").click(function(){
    confirm ("Are you ready?");
    $("#form")[0].submit();
});

Now any (after OK and Cancel) forms of time are sent.

I try that this should work logically.

$("#button").click(function(){
    confirm ($("#form")[0].submit(););
});
+4
source share
2 answers

You can call event.preventDefault()when you do not want the form to be submitted.

$("#button").click(function(event){
    if(!confirm ("your message"))
       event.preventDefault();
});

event.preventDefault ()

Description . If this method is called, the default action for the event will not fire.

+3
source

Adding if you need to do it for you

$("#button").click(function(){
if(confirm ("Are you ready?"))
    $("#form")[0].submit();
});
+2
source

Source: https://habr.com/ru/post/1547378/


All Articles