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)
精彩评论