我们在开发的时候常常需要按一个按钮然后提交多个表单,在这里我在body中设置两个表单,在按表单1的提交按钮时一起提交
表单1和表单2:
test.html:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>用户登录界面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript">
function submitit(){
//第一个表单
var tform1= document.getElementById("action1");
//第二个表单
var tform2= document.getElementById("action2");
//提交第一个表单
tform1.submit();
//提交第二个表单
tform2.submit();
}
</script>
</head>
<body>
<form id="action1" name="foration1" action="/maven-web/login" method="post" >
用户名:<input type="text" name="userName"/><br/>
密码:<input type="password" name="password"/><br/>
<input type="submit" value="提交" onClick="submitit();"/>
</form>
<form id="action2" name="foration2" action="/maven-web/login2" method="post">
用户名2:<input type="text" name="userName1"/><br/>
密码2:<input type="password" name="password1"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
关键点:
通过表单id获取表单对象,通过onClick事件和submit()函数提交多个表单