只需单击Jquery即可提交多个具有相同名称的表单

时间:2013-11-06 05:56:36

标签: php jquery forms submit

嗨我有一个循环创建的表单,我需要一键提交所有这些表单, 我正在下面的代码,请你建议我怎么做这个 谢谢

 <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                $("#submitButton").click(function () {
                    $.post($("#form1").attr("action"), $("#form1").serialize(),
                      function () {
                          alert('Form 1 submitted');
                      });

                    $.post($("#form2").attr("action"), $("#form1").serialize(),
                      function () {
                          alert('Form 2 submitted');
                      });
                });
            });
        </script>
    </head>
    <body>
        <form id="form1" action="PostPage1.aspx" method="post">
            <input type="button" id="submitButton" value="Submit" />
        </form>

        <!-- Hidden form -->
        <form id="form2" action="PostPage2.aspx" name= "formsub"  method="post">
            <input type="test" id="data1" value="testing1" />
            <input type="text" id="data2" value="testing2" />
        </form>

        <form id="form2" action="PostPage2.aspx" name= "formsub" method="post">
            <input type="test" id="data1" value="testing1" />
            <input type="text" id="data2" value="testing2" />
        </form>

    </body>
    </html>

1 个答案:

答案 0 :(得分:5)

您可以使用给定名称迭代所有表单并逐个提交

$(document).ready(function () {
    $("#submitButton").click(function () {
        var $form1 = $("#form1");
        $.post($form1.attr("action"), $form1.serialize(), function () {
            alert('Form 1 submitted');
        });

        $('form[name="formsub"]').each(function () {
            var $form = $(this);
            $.post($form.attr("action"), $form.serialize(), function () {
                alert('Form 2 submitted');
            });
        })
    });
});