如何在一个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"  οnclick="act1()"  value="第一个提交" />
            <input type="button"  οnclick="act2()"   value="第二个提交" />
</form> 

至此完毕。

转载于:https://www.cnblogs.com/tytr/p/5897331.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值