如何控制一个table动态插入/删除一个tr内容?

oldsky 2003-09-18 11:33:58
如题,就是临时插入一行tr内容,也可以删除表中的一个tr行。我记得别人做过这样的东东,现在一下子找不到了,各位帮帮忙!
...全文
225 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
alan815 2003-09-18
  • 打赏
  • 举报
回复
tigerwen01(小虎)(编程艺术化)
不错的代码,喜欢
possible_Y 2003-09-18
  • 打赏
  • 举报
回复
<table id=r>
<tr>
<td>aaa</td>
<td>bbb</td>
</tr>
</table>
<input type=button name=bt1 value=添加一行 onclick="ad(r)">
<script>
function ad(t){
tr=t.insertRow()
for(i=0;i<t.rows[0].cells.length;i++)tr.insertCell().innerHTML='possible'
}
</script>
zhangking 2003-09-18
  • 打赏
  • 举报
回复
tigerwen01(小虎) 的代码很好!!!
tigerwen01 2003-09-18
  • 打赏
  • 举报
回复
[转载]
<HTML>
<HEAD>
<TITLE>真正意义上的动态插入表格</TITLE>
<META NAME="Generator" CONTENT="Microsoft FrontPage 4.0">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY>
<TABLE width=400 id="mytable" border=1>
<TR>
<TD width="121">
<input type="checkbox" name="chk" value="">
<input type="text" name="txt_0" size="8">
</TD>
<TD width="84">
<input type="text" name="txt_1" size="8">
</TD>
<TD width="83">
<input type="text" name="txt_2" size="8">
</TD>
<TD width="84">
<input type="text" name="txt_3" size="8">
</TD>
</TR>
</TABLE>
<input type="button" value="插入下行" onClick="add_tr(1)" name="button">
<input type="button" value="插入上行" onClick="add_tr(0)" name="button">
<input type="button" value="删除行" onClick="del_tr()" name="button2">
<input type="button" name="move" value="上移" onclick="move_txt(-1)">
<input type="button" name="move2" value="下移" onClick="move_txt(1)">
</BODY>
</HTML>
<SCRIPT LANGUAGE="vbScript">
<!--
function get_chk(i,mc)
'检查所要检查的长度是多少,如果为1,则直接检查;如果大于1,则循环引用索引检查
'这个是一个特殊的方式,不知道为什么。
'返回值是停止的数值i,如果选中,则返回值为空
set my_chk=document.all(mc)
get_chk=""
'如果只有一行,则用直选,如有选中,则返回值为0
if i=0 then
if my_chk.checked then
get_chk=0
end if
else
for j=0 to i
if my_chk(j).checked then
get_chk=j
exit for
end if
next
end if
end function
function move_txt(a)
'移动行时,如果移动到-1的位置,则其实是跑到最后面一行,但如果移动到最后一行再下移则出错
set my_tab=document.all("mytable")
set my_chk=document.all("chk")
b=get_chk(my_tab.rows.length-1,"chk")
if b<>"" and b<>"0" then
if (b+a)<=(my_tab.rows.length-1) then
my_tab.moverow b,b+a
if (b+a)>=0 then
my_chk(b+a).checked=true
else
my_chk(my_chk.length-1).checked=true
end if
else
my_tab.moverow b,0
my_chk(0).checked=true
end if
else
if b="0" and my_tab.rows.length>1 then
if a="1" then
my_tab.moverow b,1
my_chk(1).checked=true
else
my_tab.moverow 0,my_tab.rows.length-1
my_chk(my_tab.rows.length-1).checked=true
end if
end if
end if
end function
function add_tr(a)
set my_tab=document.all("mytable")
set my_chk=document.all("chk")
tab_rows=my_tab.rows.length '现有行数
tab_cells=my_tab.cells.length '现有列数
b=get_chk(my_tab.rows.length-1,"chk")
'如果有被选中的行,则根据传递来的参数判断是在行前插入或在行后插入;如果没有选中,则自然插入
if b<>"" then
'如果选中的是第一行而又是向上插入的话,那么,先在该行之后插入一行,再将两者调换
if b="0" and a="0" then
set new_row=my_tab.insertrow(1)
sf_move="1"
else
set new_row=my_tab.insertRow(b+a) '插入一行
end if
else
set new_row=my_tab.insertRow(tab_rows)
end if
'在新插入的行中再循环插入列,并赋值
for i=0 to my_tab.rows(0).cells.length-1
Set new_cell=new_row.insertcell()
if i=0 then
new_cell.InnerHtml="<input type=checkbox name=chk>"&chr(13)&"<input type='text' name=txt_0 size='8'>"
else
new_cell.InnerHtml="<input type='text' name=txt_"&i&" size='8'>"
end if
next
if sf_move="1" then
my_tab.moverow 1,0
if my_tab.rows.length<=2 then
my_chk.checked=true
else
my_chk(1).checked=true
end if
end if
end function
'检测某行的所有输入框是否有值
function txt_null(s,i)
'如果传递来的选择框行数只有1行,则直接选;否则用索引方式
txt_null=false
if s="0" then
for j=0 to 3
if document.all("txt_"&j).value<>"" then
txt_null=true
exit for
end if
next
else
for j=0 to 3
if document.all("txt_"&j)(i).value<>"" then
txt_null=true
exit for
end if
next
end if
end function
function del_tr()
set my_chk=document.all("chk")
set my_tab=document.all("mytable")
if my_tab.rows.length>1 then
for i=my_chk.length-1 to 0 step -1 '倒着删除
if my_chk(i).checked then '检测该行中的输入框内有没有值,有则退出循环并将变量赋值为1
if txt_null(my_chk.length-1,i)=true then '如果该行有值
if msgbox("第"&i+1&"行中的输入框内有值,确认删除吗?",1,"")=1 then
my_tab.deleterow(i)
else
my_chk(i).checked=false
end if
else
my_tab.deleterow(i)
end if
end if
next
end if
end function
//-->
</SCRIPT>
oldsky 2003-09-18
  • 打赏
  • 举报
回复
td里面用可以,tr和table里面用就报错了
par 2003-09-18
  • 打赏
  • 举报
回复

window.upid.innerHTML=str+'<br>';




<td align="left" id="upid" height="122"> 文件1:
<input type="file" name="file1" style="width:400" class="tx1" value="">
</td>

par 2003-09-18
  • 打赏
  • 举报
回复
化境编程界文件上传就是啦

28,409

社区成员

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

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