<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="JS/jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/ecmascript">
        function Fun() {
            alert("验证");
            return true;
        }
        function test() {
            //$("#MyForm").submit();  //运行验证方法??
            document.getElementById("MyForm").submit(); //不运行验证方法??
        }
    </script>
</head>
<body>
<form id="MyForm" action="" onsubmit="return Fun();">
<input type="button" value="Jq_submit" onclick="test();" />
<input type="submit" value="submit" />
</form>
</body>
</html>为什么$("#MyForm").submit(); 运行验证方法Fun,而document.getElementById("MyForm").submit()不运行?

解决方案 »

  1.   

    因为你不用jquery的submit它就不会再你submit之前去触发表单的onsubmit事件, 除非像下面你手动去触发,这样写也可以用你的javascript方法直接触发,否则没那么只能还帮你自动触发onsubmit    <form id="MyForm" action="" onsubmit="return Fun();">
        <input type="button" value="Jq_submit" onclick="test();" />
        <input type="submit" value="submit" />
        </form>
        <script type="text/ecmascript">
            function Fun() {
                alert("验证");
                return true;
            }
            function test() {
                //$("#MyForm").submit();  //运行验证方法??
                //            document.getElementById("MyForm").submit(); //不运行验证方法??
                document.getElementById("MyForm").onsubmit();
            }
        </script>
      

  2.   

    onsubmit="return Fun();这里说明在提交表单的时候会触发。而test方法在提交表单的时候并没有谁告诉他要去触发