自己写的多表单插入数据和几个form同时提交

<%@ page language="java" contentType="text/html; charset=Shift_JIS"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/cop.tld" prefix="cop"%>
<cop:iniCtx id="ctx" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">

<script type="text/javascript" src="<%=ctx%>/js/common.js"></script>
<link href="<%=ctx%>/css/default.css" rel="stylesheet" type="text/css">
<title>Insert title here</title>
<script type="text/javascript">
function submitAll()
{
	submitMultiForm();
	var forms=document.forms;
	for(var i=0;i<forms.length;i++)
	{
		forms[i].submit();
	}
}
function submitMultiForm()
{
	var projects=document.getElementsByTagName("input");
	var str="";
	var data=[];
	var j=0;
	var count=0;
	for(var k=0;k<projects.length;k++)//这里不用用  pro in projects,获得的pro指的是数组的属性、。
	{//每3个值进行一次轮回,重新组成字符串放到数组里面,作为一行的数据
		if(count>2){data[j]=str;j++;count=0;k--;str="";continue;}
//由于需要用,连接的数据紧靠,所以Count每从0-2是一行的数组字符串的组合
		if(projects[k].id.indexOf('projectName')>=0)
		{
			str+=projects[k].value+",";
			count++;
		}
		if(projects[k].id.indexOf('isNeed')>=0)
		{	str+=projects[k].value+",";
			
			count++;
		}
		
		if(projects[k].id.indexOf('wordsCount')>=0)
		{	str+=projects[k].value+",";
			count++;
		}
	}
	//alert(k);这里K仍然有效
	projects=document.getElementsByTagName("select");
	j=0;
	for(var i=0;i<projects.length;i++)
	{
		if(projects[i].id.indexOf('wordTypeId')>=0)
		{
			//alert(projects[i].value);
			//data[j]+=projects[i].value;
			data[j]=data[j].concat(projects[i].value);
			//data[j]=data[j]+projects[i].value;
			j++;
		}
	}
	document.getElementById("hiddenData").value=data.join("|");
//对|解析的时候要用(\\|)
	//alert(document.getElementById("hiddenData").value);
}
</script>
</head>
<body>
	<fieldset>
		<legend>ログインポリシ</legend>
		<form action="<%=ctx%>/policy/loginPolicy.do" method="post">
		<table width="60%" border="0" cellspacing="1" cellpadding="3" class="contentTable">
			<tr class="defaultBGColor">
				<td class="editLabel">
					パスワード有効期間:
				</td>
				<td><input type="text" id="passwordEffective" name="passwordEffective" class="stedit"/>&nbsp;日間 (0~365)</td>
			</tr>
			<tr class="defaultBGColor">
				<td class="editLabel">
					パス-ワード変更画面表示:
				</td>
				<td><input type="text" id="passwordChange" name="passwordChange" class="stedit"/>&nbsp;日前から</td>
			</tr>
			<tr class="defaultBGColor">
				<td class="editLabel">
					ログイン有効回数:
				</td>
				<td><input type="text" id="loginTime" name="loginTime" class="stedit"/>&nbsp;回 (0~30)</td>
			</tr>
			</table>
			</form>
	</fieldset>
	<fieldset>
		<legend>ユーザ情報ポリシー</legend>
		<form action="<%=ctx%>/loginpolicy/userPolicy.do" method="post">
		<table width="60%" border="0" cellspacing="1" cellpadding="3" class="contentTable">
			<tr class="defaultBGColor">
				<td class="editLabel">
					ログインID最小桁数:
				</td>
				<td><input type="text" class="stedit" name="loginMin"/>&nbsp;文字 (1~64)</td>
			</tr>
			<tr class="defaultBGColor">
				<td class="editLabel">
					パスワード最小桁数:
				</td>
				<td><input type="text" class="stedit" name="passwordMin"/>&nbsp;文字 (1~64)</td>
			</tr>
		</table>
		<br>
		<table width="60%" border="0" cellspacing="1" cellpadding="3" class="contentTable">
			<tr class="defaultBGColor">
				<td class="editLabel" align="center">入力項目:</td>
				<td class="editLabel" align="center">項目名</td>
				<td class="editLabel" align="center">必須</td>
				<td class="editLabel" align="center">文字種</td>
				<td class="editLabel" align="center">文字数</td>
			</tr>
			<tr class="defaultBGColor">
				<td class="editLabel" align="center"> 項目1:</td>
				<td><input type="text" class="stedit" id="projectName" name="projectName"/></td>
				<td><input type="checkbox" name="isNeed" id="isNeed"/></td>
				<td><select id="wordTypeId" name="wordTypeId"><option value="1">1</option><option value="2">2</option></select></td>
				<td><input type="text" class="stedit" id="wordsCount" name="wordsCount"/></td>
			</tr>
			<tr class="defaultBGColor">
				<td class="editLabel" align="center"> 項目1:</td>
				<td><input type="text" class="stedit" id="projectName" name="projectName"/></td>
				<td><input type="checkbox" name="isNeed" id="isNeed"/></td>
				<td><select id="wordTypeId" name="wordTypeId"><option value="1">1</option><option value="2">2</option></select></td>
				<td><input type="text" class="stedit" id="wordsCount" name="wordsCount"/></td>
			</tr>
			<tr class="defaultBGColor">
				<td class="editLabel" align="center"> 項目1:</td>
				<td><input type="text" class="stedit" id="projectName" name="projectName"/></td>
				<td><input type="checkbox" name="isNeed" id="isNeed"/></td>
				<td><select id="wordTypeId" name="wordTypeId"><option value="1">1</option><option value="2">2</option></select></td>
				<td><input type="text" class="stedit" id="wordsCount" name="wordsCount"/></td>
			</tr>
			<tr><td><input type="hidden" id="hiddenData" name="hiddenData" value=""/></td></tr>
		</table>
		</form>
	</fieldset>
	<div><input type="button" value="保存" onclick="submitAll()"/><input type="button" value="キャンセル"></div>
</body>
</html>

你可能感兴趣的:(form)