Detecting if onsubmit was cancelled by another event handler?
I would l开发者_StackOverflow社区ike a way of detecting/triggering a function when the form onsubmit
is cancelled by any onsubmit
handler. What's the most reliable method for doing this?
Wrap it up...
// This code should execute last (after onsubmit was already assigned)
var oldsub = document.forms[0].onsubmit;
document.forms[0].onsubmit = function() {
if(!oldsub)
alert("Onsubmit did not exist!");
else if(oldsub())
alert("Onsubmit passed!");
else
alert("Onsubmit failed!");
}
you could override all the forms' onsubmit
handlers with your own:
var f = document.getElementById('myForm'); // or whatever
if (f.onsubmit) {
var oldSubmit = f.onsubmit;
f.onsubmit = function () {
var result = oldSubmit.call(this);
if (result === false) {
alert("Cancelled.");
}
return result;
};
}
I'm probably wrong, but would returning false
on one handler cancel the stack? Failing that, you could attach a check call on each event to check whether another one in the stack canceled.
精彩评论