动态表格保存到数据库的问题

zy8870195 2009-04-09 08:42:54
<table width="99%" border="1" align="center" cellpadding="4" cellspacing="1" class="toptable grid" id="SignFrame">           
<tr id="trHeader">
<td width="30" align="center">序号</td>
<td width="28" align="center">数量</td>
<td width="33" align="center">单价</td>
<td width="35" align="center">金额</td>
<td width="37" align="center"> </td>
</tr>
</table>
<div><br><br>
<input name="Submit" type="button" class="hand" onClick="AddSignRow()" value="添加一行" />
<input name='txtTRLastIndex' type='hidden' id='txtTRLastIndex' value="1" />                            
<input name="button" type="submit" class="hand" id="button" value=" 确认提交 "><br>
</div>
</body>
</HTML>
<%
Dim count1,rsClass1,sqlClass1
set rsClass1=server.createobject("adodb.recordset")
sqlClass1="select * from cgdlei Order By lei asc"
rsClass1.open sqlClass1,conn,1,1
%>
<script language="javascript">// Example: obj = findObj("image1");
var arr1 = [], arr2 = [];
function findObj(theObj, theDoc){ var p, i, foundObj; if(!theDoc) theDoc = document; if( (p = theObj.indexOf("?")) > 0 && parent.frames.length) { theDoc = parent.frames[theObj.substring(p+1)].document; theObjtheObj = theObj.substring(0,p); } if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj]; for (i=0; !foundObj && i < theDoc.forms.length; i++) foundObj = theDoc.forms[i][theObj]; for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) foundObj = findObj(theObj,theDoc.layers[i].document); if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj); return foundObj;}
//添加一个填写行
function AddSignRow(){ //读取最后一行的行号,存放在txtTRLastIndex文本框中
var txtTRLastIndex = findObj("txtTRLastIndex",document);
var rowID = parseInt(txtTRLastIndex.value);

var signFrame = findObj("SignFrame",document);
//添加行
var newTR = signFrame.insertRow(signFrame.rows.length);
newTR.id = "SignItem" + rowID;

//添加列:序号
var newNameTD=newTR.insertCell(0);
//添加列内容
newNameTD.innerHTML = newTR.rowIndex.toString();

//添加列:数量
var newCompanyTD=newTR.insertCell(1);
//添加列内容
newCompanyTD.innerHTML = "<input onFocus='this.select()' name='sl" + rowID + "' type='text' class='txt24px' id='sl" + rowID + "' onKeypress='javascript:if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;' value='0' size='2' style='border: #FF0000 1px solid;' onchange='co(\"ss\"," + rowID + ",this.value)'/>";

//添加列:单价
var newCompanyTD=newTR.insertCell(2);
//添加列内容
newCompanyTD.innerHTML = "<input onFocus='this.select()' name='dj" + rowID + "' type='text' class='txt24px' id='dj" + rowID + "' onKeypress='javascript:if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;' value='0' size='2' style='border: #FF0000 1px solid;' onchange='co(\"dj\"," + rowID + ",this.value)'/>";

//添加列:金额
var newCompanyTD=newTR.insertCell(3);
//添加列内容
newCompanyTD.innerHTML = "<input name='jine" + rowID + "' readonly='ture' type='text' class='txt24px' id='jine" + rowID + "' onKeypress='javascript:if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;' size='4' value='0' />";

//添加列:删除按钮
var newDeleteTD=newTR.insertCell(4);
//添加列内容
newDeleteTD.innerHTML = "<div align='center' style='width:40px'><a href='javascript:;' onclick=\"DeleteSignRow('SignItem" + rowID + "')\">删除</a></div>";

//将行号推进下一行
txtTRLastIndex.value = (rowID + 1).toString() ;
}
//删除指定行
function DeleteSignRow(rowid){
var signFrame = findObj("SignFrame",document);
var signItem = findObj(rowid,document);

//获取将要删除的行的Index
var rowIndex = signItem.rowIndex;

//删除指定Index的行
signFrame.deleteRow(rowIndex);

//重新排列序号,如果没有序号,这一步省略
for(i=rowIndex;i<signFrame.rows.length;i++){
signFrame.rows[i].cells[0].innerHTML = i.toString();
}
}//清空列表
function ClearAllSign(){
if(confirm('确定要清空所有吗?')){
var signFrame = findObj("SignFrame",document);
var rowscount = signFrame.rows.length;

//循环删除行,从最后一行往前删除
for(i=rowscount - 1;i > 0; i--){
signFrame.deleteRow(i);
}

//重置最后行号为1
var txtTRLastIndex = findObj("txtTRLastIndex",document);
txtTRLastIndex.value = "1";

//预添加一行
AddSignRow();
}
}
function co(a,b,c){
if (a=="ss") {
arr1[b] = c;
//alert("第"+b+"行的数量="+c);
}
if (a=="dj") {
arr2[b] = c;
//alert("第"+b+"行的单价="+c);
}
if (arr1[b] && arr2[b]){
//document.getElementsByName("je")[b-1].value = arr1[b]*arr2[b];
document.getElementById("jine"+b).value = arr1[b]*arr2[b];
}
}
</script>
...全文
131 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wei809206538 2012-05-28
  • 打赏
  • 举报
回复
小猪,这个问题你解决了吗,我也要做一个JS动态生成表格,提交到数据库中,网上找了好多,都不行,你会了吗
  • 打赏
  • 举报
回复
如果你创建的是相同name的表单,还可以这么用

if request.Form.count>0 then
for i=1 to Request.Form("sl").Count
response.Write request.Form("sl")(i)&"<br>"
response.Write request.Form("dj")(i)&"<br>"
response.Write request.Form("jine")(i)&"<br>"
next
end if
  • 打赏
  • 举报
回复
呵呵,你用
if request.Form.count>0 then
response.Write request.Form
end if

这样一个输出,你就可以看到一串字符串里,里面包含里你要的信息
  • 打赏
  • 举报
回复
呵呵,有2种方法可以解决
1种是在服务端修改用法
1种是修改JS客户端代码
zy8870195 2009-04-09
  • 打赏
  • 举报
回复
<%
lines=request.form("txtTRLastIndex")-1

dim xm_array1(),xm_array2(),xm_array3()

redim xm_array1(lines+1),xm_array2(lines+1),xm_array3(lines+1)

xm_array6(i)= request.form("sl"+cstr(i))'数量
xm_array7(i)= request.form("dj"+cstr(i))'单价
xm_array8(i)= request.form("je"+cstr(i))'金额

sl=xm_array6(i)'数量
dj=xm_array7(i)'单价
je=xm_array8(i)'金额

exec="insert into cgd([sl],[dj],[je]) values('"+sl+"','"+dj+"','"+je+"')"
conn.execute (exec)

next
conn.close
set conn=nothing
%>
如果删除其中一行 写入的时候 数据库会有这一行空白的信息
如何解决啊
  • 打赏
  • 举报
回复
我只说思路
不帖代码

已经说得很清楚了

如果你连说得这么明白的话都看不懂

我也不想说什么了
zy8870195 2009-04-09
  • 打赏
  • 举报
回复
<% 
lines=request.form("txtTRLastIndex")-1

dim xm_array1(),xm_array2(),xm_array3()

redim xm_array1(lines+1),xm_array2(lines+1),xm_array3(lines+1)

for i=1 to lines
xm_array6(i)= request.form("sl"+cstr(i))'数量
xm_array7(i)= request.form("dj"+cstr(i))'单价
xm_array8(i)= request.form("je"+cstr(i))'金额
next

数据库连接

for i=1 to lines
sl=xm_array6(i)'数量
dj=xm_array7(i)'单价
je=xm_array8(i)'金额

exec="insert into cgd([sl],[dj],[je]) values('"+sl+"','"+dj+"','"+je+"')"
conn.execute (exec)

next
conn.close
set conn=nothing
%>
saltprune 2009-04-09
  • 打赏
  • 举报
回复
把代码贴的完整点,都没法调试。

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧