javascript中怎么实现表格行的批量删除
用javascript语言实现,就是每一行有个复选框,下面有个按钮,点按钮可以把复选框选中的行删除掉。怎么实现啊,最好能给短代码...
用javascript语言实现,就是每一行有个复选框,下面有个按钮,点按钮可以把复选框选中的行删除掉。怎么实现啊,最好能给短代码
展开
1个回答
2011-01-07
展开全部
给你代码:
<!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>DOM动态删除TABLE多行</title>
<script type="text/javascript">
function $(objId){
return document.getElementById(objId);
}
function del_tbl(tblN,ckN){
var ck = document.getElementsByName(ckN);
var tab = $(tblN);
var rowIndex;
for(var i=0;i<ck.length;i++){
if(ck[i].checked){
rowIndex = ck[i].parentNode.parentNode.sectionRowIndex;
tab.deleteRow(rowIndex);
i = -1;
}
}
}</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<table width="253" border="1" align="center" id="list">
<tr>
<td><input type="checkbox" name="cb_k" id="cb_k" /></td>
<td>2</td>
<td>3</td>
</tr>
<tr id="tr1">
<td width="28" height="41"><input type="checkbox" name="del" id="del" /></td>
<td width="124"><div align="center">第一行 </div></td>
<td width="79"><div align="center">1</div></td>
</tr>
<tr id="tr2">
<td height="36"><input type="checkbox" name="del" id="del" /></td>
<td><div align="center">第二行 </div></td>
<td><div align="center">2</div></td>
</tr>
<tr id="tr3">
<td height="40"><input type="checkbox" name="del" id="del" /></td>
<td><div align="center">第三行 </div></td>
<td><div align="center">3</div></td>
</tr>
<tr id="tr4">
<td height="37"><input type="checkbox" name="del" id="del" /></td>
<td><div align="center">第四行 </div></td>
<td><div align="center">4</div></td>
</tr>
<tr id="tr5">
<td height="50"><input type="checkbox" name="del" id="del" /></td>
<td><div align="center">第五行 </div></td>
<td><div align="center">5</div></td>
</tr>
<tr>
<td colspan="3">
<div align="center">
<input type="button" name="btn_del" id="btn_del" value="删 除" onclick="del_tbl('list','del')"/>
</div></td>
</tr>
</table>
</form>
</body>
</html>
<!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>DOM动态删除TABLE多行</title>
<script type="text/javascript">
function $(objId){
return document.getElementById(objId);
}
function del_tbl(tblN,ckN){
var ck = document.getElementsByName(ckN);
var tab = $(tblN);
var rowIndex;
for(var i=0;i<ck.length;i++){
if(ck[i].checked){
rowIndex = ck[i].parentNode.parentNode.sectionRowIndex;
tab.deleteRow(rowIndex);
i = -1;
}
}
}</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<table width="253" border="1" align="center" id="list">
<tr>
<td><input type="checkbox" name="cb_k" id="cb_k" /></td>
<td>2</td>
<td>3</td>
</tr>
<tr id="tr1">
<td width="28" height="41"><input type="checkbox" name="del" id="del" /></td>
<td width="124"><div align="center">第一行 </div></td>
<td width="79"><div align="center">1</div></td>
</tr>
<tr id="tr2">
<td height="36"><input type="checkbox" name="del" id="del" /></td>
<td><div align="center">第二行 </div></td>
<td><div align="center">2</div></td>
</tr>
<tr id="tr3">
<td height="40"><input type="checkbox" name="del" id="del" /></td>
<td><div align="center">第三行 </div></td>
<td><div align="center">3</div></td>
</tr>
<tr id="tr4">
<td height="37"><input type="checkbox" name="del" id="del" /></td>
<td><div align="center">第四行 </div></td>
<td><div align="center">4</div></td>
</tr>
<tr id="tr5">
<td height="50"><input type="checkbox" name="del" id="del" /></td>
<td><div align="center">第五行 </div></td>
<td><div align="center">5</div></td>
</tr>
<tr>
<td colspan="3">
<div align="center">
<input type="button" name="btn_del" id="btn_del" value="删 除" onclick="del_tbl('list','del')"/>
</div></td>
</tr>
</table>
</form>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询