How do I prevent a programmatic form submit from JS?
I have an external library that's calling form.submit()
. No matter what I do, I can't seem to catch the event when it's called directly like that.
Example: http://jsfiddle.net/cwolves/开发者_如何学运维yXsWc/
Instead of intercepting the event, have you tried intercepting the submit()
call itself? You could do something like replace the default submit()
function with one of your choosing that only submits if some flag is set. For instance:
var formElem = document.getElementById("myForm");
formElem.oldSubmit = formElem.submit;
formElem.submit = function(myFlag) {
if (myFlag) {
document.getElementById("myForm").oldSubmit();
}
};
This might be a bit hack-ish, but you could unbind the click
event from that certain element, then re-bind it to work the way you want it to.
$('button').unbind('click').click(form.onsubmit);
jsFiddle
精彩评论