欢迎访问爱油菜中文网!
您所在的位置:首页 > 代码学苑 > 网建教程

html如何在一个form表单中实现多个submit

作者:来源:发表于:2022-02-09 13:01:59

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> 

至此完毕。