如何重新启用 event?

我有一个网页,我已经防止默认行动的所有提交按钮,但我想重新启用默认提交按钮的行动,我怎么能这样做?

我目前正在使用以下方法防止默认操作:

$("form").bind("submit", function(e){
e.preventDefault();
});

我通过以下方法成功地做到了这一点:

$(document).ready(function(){
$("form:not('#press')").bind("submit", function(e){
e.preventDefault();
});

但是,当按钮被点击时,我能动态地做到这一点吗?

155558 次浏览

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;
$('form').submit( function(e){


e.preventDefault();


//later you decide you want to submit
$(this).trigger('submit');     or     $(this).trigger('anyEvent');
function(e){ e.preventDefault();

and its opposite

function(e){ return true; }

cheers!

You can re-activate the actions by adding

this.delegateEvents();  // Re-activates the events for all the buttons

If you add it to the render function of a backbone js view, then you can use event.preventDefault() as required.

With async actions (timers, ajax) you can override the property isDefaultPrevented like this:

$('a').click(function(evt){
e.preventDefault();


// in async handler (ajax/timer) do these actions:
setTimeout(function(){
// override prevented flag to prevent jquery from discarding event
evt.isDefaultPrevented = function(){ return false; }
// retrigger with the exactly same event data
$(this).trigger(evt);
}, 1000);
}

This is most complete way of retriggering the event with the exactly same data.

I had a similar problem recently. I had a form and PHP function that to be run once the form is submitted. However, I needed to run a javascript first.

        // This variable is used in order to determine if we already did our js fun
var window.alreadyClicked = "NO"
$("form:not('#press')").bind("submit", function(e){
// Check if we already run js part
if(window.alreadyClicked == "NO"){
// Prevent page refresh
e.preventDefault();
// Change variable value so next time we submit the form the js wont run
window.alreadyClicked = "YES"
// Here is your actual js you need to run before doing the php part
xxxxxxxxxx


// Submit the form again but since we changed the value of our variable js wont be run and page can reload (and php can do whatever you told it to)
$("form:not('#press')").submit()
}
});