从其他库提交时提取表单提交

时间:2010-10-15 21:46:28

标签: jquery

当根据按钮点击事件动态从另一个javascript库触发该事件时,是否可以捕获表单提交事件?

1 个答案:

答案 0 :(得分:0)

您可以尝试绑定提交事件监听器 示例html:

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

示例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);​

​

您可以测试此jsFiddle live example

单击按钮(或直接在DOM节点上调用.click())或使用$.submit触发拦截器事件,但直接使用$.click.submit()调用表单DOM节点失败 所以1和4工作,2和3不   (需要更多调查)