</pre>
Javascript 动态多行输入问题:
//input.jsp
<%@ page contentType="text/html;charset=Big5"%>
<%request.setCharacterEncoding("big5");%>
<script language="javascript">
function tbladdrow()
{
var i=lines.rows.length;
var row = Table1.insertRow(Table1.rows.length);
var col = row.insertCell(0);
col.innerHTML = "<INPUT id=line["+i+"][name1] name=line["+i+"]name1 SIZE=10>";
col = row.insertCell(1);
col.innerHTML = "<INPUT id=line["+i+"][name2] name=line["+i+"]name2 SIZE=10>";
col = row.insertCell(2);
col.innerHTML = "<INPUT id=line["+i+"][name3] name=line["+i+"]name3 SIZE=10>";
col = row.insertCell(3);
col.innerHTML = "<INPUT id=line["+i+"][name4] name=line["+i+"]name4 SIZE=10>";
}
function tbladdrows(items)
{
for( i = 1 ; i <= items ; i++)
{
tbladdrow();
}
}
function delrow()
{
if(lines.rows.length==0)
{
return false ;
}
lines.deleteRow();
}
</script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Big5">
<title>輸入</title>
</head>
<body>
<form action="getInput.jsp">
<DIV align=center><FONT size=2>輸入測試</FONT></DIV></TD>
<TD width=412 bgColor=#e4e4e4>
<TABLE id=Table1 cellSpacing=0 borderColorDark=#ffffff cellPadding=0
width=350 align=center borderColorLight=#000000 border=1>
<TBODY>
<TR bgColor=#999999>
<TH width=94><FONT size=2>參數一</FONT></TH>
<TH width=84><FONT size=2>參數二</FONT></TH>
<TH width=85><FONT size=2>參數三</FONT></TH>
<TH width=77><FONT size=2>參數四</FONT></TH></TR>
<TBODY id=lines name="lines"><!--
<TR>
<TD><INPUT id="things_name" TYPE="text" NAME="things_name" SIZE="15"></TD>
<TD><INPUT id="things_model" TYPE="text" NAME="things_model" SIZE="10"></TD>
<TD><INPUT id="things_number" TYPE="text" NAME="things_number" SIZE="5"></TD>
<TD><input id="things_unit" type="text" name="things_unit" size="5"></TD>
</TR>
--></TBODY></TABLE>
<DIV align=center><BR></DIV>
<DIV align=center>
<INPUT id=items type=hidden value=1 name=items> <INPUT onclick=tbladdrow(items.value); type=button value=新增一行 name=insert/>
<font color="#e4e4e4"> </font>
<INPUT language=javascript onclick="return delrow()" type=button value=刪除一行 name=del/>
</DIV></TD></TR></TBODY></TABLE>
<input type="submit" value="commit"/>
</form>
</body>
</html>
//getInput.jsp
<%@ page contentType="text/html;charset=Big5"%>
<%@ page import="java.util.*"%>
<%
request.setCharacterEncoding("big5");
//for(){
/*int items =Integer.parseInt(request.getParameter("items"));
out.println(items);
for(int i=1; i<=items; i++){*/
//get the user's input text name
Enumeration paramNames=request.getParameterNames();
String paramName=null;
while(paramNames.hasMoreElements()){
paramName=(String)paramNames.nextElement();
String paramValues=request.getParameter(paramName);
out.println("<font color=red>param'name </font>"+paramName+" ");
out.println("<font color=red>paramValues</font>"+paramValues+"<br>");
//out.println("***<br>");
/*String param=request.getParameter(paramName);
out.println(paramName+" "+param+"<br>");*/
/*String paramValues[]=request.getParameterValues(paramName);
for(int i=0; i<=paramValues.length-1; i++)
{
out.println("<font color=red>param'name </font>"+paramName+" ");
out.println("<font color=red>pramValues["+i+"]</font>"+paramValues[i]+"<br>");}
*/
}
//out.println("<br>"+name);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Big5">
<title>接受輸入</title>
</head>
<body>
</body>
</html>
运行效果:
param'name line[0]name2 paramValues12
param'name line[0]name3 paramValues13
param'name items paramValues1
param'name line[0]name1 paramValues11
param'name line[1]name4 paramValues24
param'name line[1]name1 paramValues21
param'name line[0]name4 paramValues14
param'name line[1]name3 paramValues23
param'name line[1]name2 paramValues22
可以看到,我们可以得到正确的值,但是,有一个问题,值是乱的,没有规律地出现,我们要把值插入数据库的时候,需要整理后再插入(废话).怎么办?
如果我们知道到底输入了几行,这个问题几乎可以迎刃而解了,可是,我们如何知道,前面的一个页面是