asp+jsp+JavaScript动态实现添加数据行(3)


代码:

<!--#include file="inc/conn.asp" -->
<%
 if request("tj")="添 加" then 
 set rs=server.CreateObject("adodb.RecordSet")
 strsql="select * from tb_person"
 rs.open strsql,conn,2,3
 for i =1 to request("username").count 
 rs.addnew()
 rs("userName")=request("username")(i)
 rs("sex")=request("usersex")(i)
 rs("age")=request("userage")(i)
 rs("aihao")=request("userlove")(i)
 rs.update
 next
 end if
 
%>
<html>
<head>
<script src="myjs.js"></script> 
</head>
<body>
<form name=frmUserInfo action="user_list1.asp" method=post> 
<br> 
<table id=tabUserInfo border=1 width="720"> 
<tr> 
 <td>姓名</td>
 <td>性别</td>
 <td>年龄</td>
 <td>爱好</td>
<td>Delete</td>
</tr> 
<tr style="display:none" id=trUserInfo> 
<td id=tdUserInfo><input id=username name=username ></td> 
<td id=tdUserInfo><input id=usersex name=usersex></td> 
<td id=tdUserInfo><input id=userage name=userage></td> 
<td id=tdUserInfo><input id=userlove name=userlove></td> 
<td id=tdUserInfo>
<img alt="Delete" onClick="deleteRow(document.all.tabUserInfo,1,this)">
</td> 
</tr> 
<tr> 
<td>

<input type=button value="Add" onClick="addRow(document.all.tabUserInfo,null,1,1)"></td> 
</tr> 
</table> 

<table> 
<tr><td><input type="submit" value="添 加" name="tj" /></td></tr> 
</table> 
</form> 
</body>

注意:在这里我们可以直接通过request("username")获得username列的数组值,通过调试可以看到,第一个(索引为0)值为",",所以获得值的时候我们要从索引为1开始读取,然后逐一添加操作即可

修改:
首先根据外键循环读取数据到一个表格里,代码:

<form name=frmUserInfo action="user_list1.asp" method=post> 
<br> 
<table border=1 width="720"> 
<tr> 
 <td>姓名</td>
 <td>性别</td>
 <td>年龄</td>
 <td>爱好</td>
<td>Delete</td> 
</tr> 
<% 
 set rs=server.CreateObject("adodb.RecordSet")
 strsql="select * from tb_person"
 rs.open strsql,conn,2,3
 do while not rs.eof 
%>
<tr id=trUserInfo1> 
<td id=tdUserInfo1><input id=username1 name=username1 value="<%=rs("userName")%>" ></td> 
<td id=tdUserInfo1><input id=usersex1 name=usersex1 value="<%=rs("age")%>"></td> 
<td id=tdUserInfo1><input id=userage1 name=userage value="<%=rs("sex")%>"></td> 
<td id=tdUserInfo1><input id=userlove1 name=userlove value="<%=rs("aihao")%>"></td> 
<td id=tdUserInfo>
<input type="submit" value="删除" />
<!--<img alt="Delete" onClick="deleteRow(document.all.tabUserInfo,1,this)"> --></td> 
</tr> 
<%
 rs.movenext
 loop
%>
<table id=tabUserInfo border=1 width="720"> 
<tr> 
 <td></td>
 <td></td>
 <td></td>
 <td></td>
<td></td> 
</tr> 
<tr style="display:none" id=trUserInfo> 
<td id=tdUserInfo><input id=username name=username ></td> 
<td id=tdUserInfo><input id=usersex name=usersex></td> 
<td id=tdUserInfo><input id=userage name=userage></td> 
<td id=tdUserInfo><input id=userlove name=userlove></td> 
<td id=tdUserInfo>
<img alt="Delete" onClick="deleteRow(document.all.tabUserInfo,1,this)">
</td> 
</tr> 
<tr> 
<td>

<input type=button value="Add" onClick="addRow(document.all.tabUserInfo,null,1,1)"></td> 
</tr> 
</table> 
      

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/2254.html