JQuery form not submit after fixing failed validation

I have a simple form that asks if you are sure you want to submit it.

$(document).ready(function(){ $("form#myform").on('submit', function(e) { if (!confirm('Are you sure?')){ e.preventDefault(); } else { console.log("submitting..."); } }); }); 

Submit the form, click "ok" at the prompt "are you sure", the form only fines.

- refresh the page, clear the slate -

Submit the form, click "Cancel" to "are you sure." The form does not appear as expected. Submit the form again, click "ok" at the prompt "Are you sure", the form is still not submitted, "Submit ..." is sent to the console. From this point on, the form will never be submitted until I refresh the page and click OK for the first time.

If I change

e.preventDefault ();

from

return false;

nothing changes, exactly the same behavior.

I am completely at a dead end. What am I doing wrong?

edit: Here is the jsFiddle link https://jsfiddle.net/8ac3Lgzg/ . The HTML and JavaScript in this fiddle are / exactly the same / as the code I have in my project. The problem described above is NOT executed when jsFiddle starts, but in my project.

+5
source share
3 answers

Your code works fine, here is the jsFiddle daemon. I literally copy and paste, there must be some other javascript affecting the form somewhere on your page.

 jsFiddle link below 

http://jsfiddle.net/xbhvqvp7/1/

You may also need to check your network logs in your browser to see if the form is actually submitted.

0
source

The problem arises from a piece of code that another developer on my team wrote that I did not know that it attached to each form and cached it as a result of executing the Submit handler. Since my form will return false if the validation fails, this result will be cached for the next attempt to submit, even if it passes the validation.

Not wanting to break his code, my solution was to add an attribute to my "no-cache" form, and I updated his method to check this attribute. This solved my problem.

Thanks to everyone who helped, I am very grateful!

0
source

try this way

 function SendForm(){ if (!confirm('Are you sure?')){ return false; } else { console.log("submitting..."); return true; } } 

put the sendForm function in a submit button like this

 <input type="submit" value="submit" onclick="return SendForm();" /> 

obviously, nothing is displayed on the console because when you submit the form the browser reloads

-2
source

All Articles