zoukankan      html  css  js  c++  java
  • 如何在一个form表单中实现多个submit

    As we all know,通过设置input type=“submit”,我们可以把form表单中的值通过post方式传递给action所指向的页面。下图中,我们可以把userName,userAge,userSex这三个值传递到xxx.jsp

    <form action="xxx.jsp" method="post">
                <input type="text" name="userName"/>
                <input type="text" name="userAge"/>
                <input type="text" name="userSex"/>
    </form> 

    但是,有些情况下(大家可能会遇到),需要在同一个form中使用两个或多个submit,当然,这在语法上是不行的,怎么办呢,我们可以通过javascript来实现。此时,from表单的写法也有些变化——(1)增加一个name属性,(2)把action属性去掉,(3)使用类型为button的input标签。 

    <form name="frm" method="post">
                <input type="text" name="userName"/>
                <input type="text" name="userAge"/>
                <input type="text" name="userSex"/>

                <input type="button" name=" value="第一个提交" />
                <input type="button" name="value="第二个提交" />
    </form> 

    接着,写javascript代码

    <script>

                 function act1(){

                           document.frm.action="xxx1.jsp";

                           document.frm.submit();   

    }

                 function act2(){

                           document.frm.action="xxx2.jsp";

                           document.frm.submit();   

    }

    </script>

    然后在button里添加onclick事件

     

    <form name="frm" method="post">
                <input type="text" name="userName"/>
                <input type="text" name="userAge"/>
                <input type="text" name="userSex"/>

                <input type="button"  onclick="act1()"  value="第一个提交" />
                <input type="button"  onclick="act2()"   value="第二个提交" />
    </form> 

    至此完毕。

  • 相关阅读:
    ZJOI2017 Day3 滚粗记
    ZJOI2017 Day2
    bzoj4245 [ONTAK2015]OR-XOR (贪心)
    bzoj4631 踩气球 (树状数组+线段树)
    bzoj5219 [Lydsy2017省队十连测]最长路径 (DP)
    bzoj5216 [Lydsy2017省队十连测]公路建设 (线段树)
    bzoj2754 [SCOI2012]喵星球上的点名 (后缀数组+树状数组)
    bzoj2342 [Shoi2011]双倍回文 (manacher)
    bzoj4657 tower (最小割)
    bzoj2064 分裂 (状压dp)
  • 原文地址:https://www.cnblogs.com/tytr/p/5897331.html
Copyright © 2011-2022 走看看