文本自动增加功能参考例子

 例子:

  table.jsp 

 

  <%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<body>
<script language="javascript">
<!--
var newItemNumber = 2;
function additem(id)
{
   var row,cell,str;
   row = document.getElementById(id).insertRow();
   if(row != null )
   {
cell = row.insertCell();
cell.innerHTML="名称:<input type=/"text/" name=/"StuName" + newItemNumber + "/">学号:<input type=/"text/" name=/"StuNames" + newItemNumber + "/"><input type=/"button/" value=/"删除/" onclick=/'deleteitem(this);/'>";
document.getElementsByName("totalNumber")[0].value = newItemNumber++;
   }
}
function deleteitem(obj)
{
   var curRow = obj.parentNode.parentNode;
   tb.deleteRow(curRow.rowIndex);
   document.getElementsByName("totalNumber")[0].value = eval(document.getElementsByName("totalNumber")[0].value) - 1;
}
//-->
</script>
<form name="form" method="post" action="addrow.jsp">
<input  type="text" name="StuName1"><br>
<table id="tb"></table>

<input name="button" type=button onClick='additem("tb")' value="添加>>">

<input type="hidden" name="totalNumber" value=""> <!-- 记录增加了多少文本框 //-->
<input type="submit" name="submit" value="提交">
</form>
</body>
</html>

 

 

addrow.jsp

 

 

  <%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>获得表单中的值</title>
</head>

<body>
<%
String num=request.getParameter("totalNumber");
int  count =0;
if(num !=null ){
   count=Integer.parseInt(num);
}
String []states=new String[count];
for(int i=0;i<count;i++){
  states[i]=request.getParameter(("StuName"+(i+1)));
  out.print(states[i]+"<br>");
}
%>
</body>
</html>

你可能感兴趣的:(String,XHTML,function,input,import,button)