开发者

How do I prevent a programmatic form submit from JS?

开发者 https://www.devze.com 2023-02-17 21:17 出处:网络
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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消