开发者

Capture form submit when submitted from another library

Is it possible to ca开发者_运维技巧pture the form submit event when that event is triggered from another javascript library dynamically based on a button click event?


You can try binding a submit event listener.
Example html:

<form id="form" action="javascript:alert('form submitted !!!')">
    <input id="btn" type="submit">
</form>

​Example javascript:

$('form').submit(function(e) {
    if (!confirm('Are you sure you want to submit?')) {
        return false;
    }
});

//1: simulate a click event triggered directly
window.setTimeout('alert("simulating click ...");document.getElementById("btn").click();', 3000);

//2: simulate a submit event triggered directly
window.setTimeout('alert("simulating submit ...");document.getElementById("form").submit();', 6000);

//3: simulate a click event triggered by jQuery
window.setTimeout('alert("simulating $.click ...");$("#btn").click();', 9000);

//4: simulate a submit event triggered by jQuery
window.setTimeout('alert("simulating $.submit ...");$("#form").submit();', 12000);​

​

You can test this jsFiddle live example.

Clicking on the button (or calling .click() directly on the DOM node) or using $.submit triggers the interceptor event, but using $.click or a .submit() call directly on the form DOM node fails.
So 1 and 4 work, 2 and 3 don't
(more investigation needed)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜