当javascript在元素上调用提交时,jQuery覆盖表单提交不起作用 [英] jQuery override form submit not working when submit called by javascript on a element

查看:135
本文介绍了当javascript在元素上调用提交时,jQuery覆盖表单提交不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有正常表单的页面,其中包含一个提交按钮和一些绑定到表单提交事件的jQuery,并用 e.preventDefault()覆盖它运行AJAX命令。单击提交按钮时工作正常,但单击 onclick ='document.formName.submit();'的链接时,AJAX不会捕获该事件表单提交事件处理程序任何想法为什么没有或如何在不绑定所有元素的情况下使其工作?

I've got a page with a normal form with a submit button and some jQuery which binds to the form submit event and overrides it with e.preventDefault() and runs an AJAX command. This works fine when the submit button is clicked but when a link with onclick='document.formName.submit();' is clicked, the event is not caught by the AJAX form submit event handler. Any ideas why not or how to get this working without binding to all the a elements?

推荐答案

一些建议:


  • 覆盖提交功能以进行恶意出价


    var oldSubmit = form.submit;
    form.submit = function() {
        $(form).trigger("submit");
        oldSubmit.call(form, arguments);
    }




  • 为什么不绑定到所有< a> ;标签?然后你不必做任何猴子修补,它可以很简单(假设所有链接都在form标签内):

  • 
        $("form a").click(function() {
            $(this).parents().filter("form").trigger("submit");
        });
    

    这篇关于当javascript在元素上调用提交时,jQuery覆盖表单提交不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆