一键提交多个具有相同名称的表单 [英] submit multiple forms with same name with one click Jquery

查看:119
本文介绍了一键提交多个具有相同名称的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态生成的循环表单,我需要一键提交所有表单, 我遵循以下代码,请给我建议我该怎么做 谢谢

Hi i have a forms in loop which are created dymanically , and i need to submit all of them on one click , i am folowing the below code , cam you please suggest me how can i do this thanks

 <!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>

推荐答案

您可以遍历具有给定名称的所有表单,然后一步一步提交

You can iterate through all the forms with the given name and submit one by one

$(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');
            });
        })
    });
});

这篇关于一键提交多个具有相同名称的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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