views:

1040

answers:

3

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?

+3  A: 

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()

  });
redsquare
Thanks for your answer, could you give me an example of how to do that.
Richbits
Thanks, thought that I had tried that, but obviously not.
Richbits
+2  A: 

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;
Patrice
A: 
$('form').submit( function(ev){

     e.preventDefault();

     //later you decide you want to submit
     $(this).trigger('submit');     or     $(this).trigger('anyEvent');
adardesign