I have a web page which I have prevented the default action on all submit buttons, however I would like to re-enable default submit action on a button how can I do this?
I am currently preventing the default action using the following:
$("form").bind("submit", function(e){ e.preventDefault(); });
I have successfully done this using the following:
$(document).ready(function(){ $("form:not('#press')").bind("submit", function(e){ e.preventDefault(); });
But can I do this dynamically when the button is clicked?
submit(function(event) { event. preventDefault(); return false; }); } }); function validateField() { var first_name = $('#first_name').
$(document). ready(function(){ $("form:not('#press')"). bind("submit", function(e){ e. preventDefault(); });
preventDefault() The preventDefault() method of the Event interface tells the user agent that if the event does not get explicitly handled, its default action should not be taken as it normally would be.
We can prevent this default behaviour by making a small modification to the definition of the handleSubmit function. We call a preventDefault on the event when submitting the form, and this will cancel the default event behavior (browser refresh) while allowing us to execute any code we write inside handleSubmit.
You would have to unbind the event and either rebind to a separate event that does not preventDefault or just call the default event yourself later in the method after unbinding. There is no magical event.cancelled=false;
As requested
$('form').submit( function(ev){ ev.preventDefault(); //later you decide you want to submit $(this).unbind('submit').submit() });
Either you do what redsquare proposes with this code:
function preventDefault(e) { e.preventDefault(); } $("form").bind("submit", preventDefault); // later, now switching back $("form#foo").unbind("submit", preventDefault);
Or you assign a form attribute whenever submission is allowed. Something like this:
function preventDefault(e) { if (event.currentTarget.allowDefault) { return; } e.preventDefault(); } $("form").bind("submit", preventDefault); // later, now allowing submissions on the form $("form#foo").get(0).allowDefault = true;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With