怎样用javascript删除用户选定的表格行?
<!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> Untitled Document </title>
<script language= "javascript ">
var rowIndex = 0;
function AddOneLine(str1)
{
var row = userlist.insertRow(userlist.rows.length);
var col = row.insertCell(0);
col.innerHTML = " <input name= 'checkbox ' type= 'checkbox ' value= ' "+rowIndex+ " ' id= 'checkbox_ "+rowIndex+ " ' /> ";
col.setAttribute( "align ", "center ");
col = row.insertCell(1);
col.innerText = rowIndex;
col.setAttribute( "align ", "center ");
row.setAttribute( "id ", "row "+rowIndex);
row.setAttribute( "name ", "row "+rowIndex);
rowIndex++;
}
//清空电话列表
function clearlist(){
var p=0;
var ls_t=document.all( "userlist ");
while (p <userlist.rows.length){
if (document.getElementById( "checkbox_ "+p).checked==true){
ls_t.deleteRow(p);
p=0;
}else{
p++;
}
}
}
</script>
</head>
<body>
<input name= "add " type= "button " onclick= "AddOneLine( '未知 ') " value= "add "/>
<input name= "del " type= "button " value= "del " onclick= "clearlist() "/>
<div style= "overflow:auto; width:100%; height:220 ">
<table width= "750 " border= "0 " cellspacing= "0 " cellpadding= "0 " id= "userlist ">
<th width= "27 "> </th>
<th width= "102 "> </th>
<th width= "80 "> </th>
<th width= "67 "> </th>
<th width= "63 "> </th>
<th width= "85 "> </th>
<th width= "119 "> </th>
<th> </th>
</table>
</div>
</body>
</html>
上面这个页面想要做成当用户选中表中的checkbox后,按del按钮会删除所有选中的数据行,可是为什么会出错呢?请高手讲讲。
[解决办法]
<!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> Untitled Document </title>
<script language= "javascript ">
var rowIndex = 0;
function AddOneLine(str1)
{
var row = userlist.insertRow(userlist.rows.length);
var col = row.insertCell(0);
col.innerHTML = " <input name= 'checkbox1 ' type= 'checkbox ' value= ' "+rowIndex+ " ' id= 'checkbox_ "+rowIndex+ " '/> ";
alert(col.innerHTML);
col.setAttribute( "align ", "center ");
col = row.insertCell(1);
col.innerText = rowIndex;
col.setAttribute( "align ", "center ");
row.setAttribute( "id ", "row "+rowIndex);
row.setAttribute( "name ", "row "+rowIndex);
rowIndex++;
}
//清空电话列表
function clearlist(){
var p=0;
var ls_t=document.all( "userlist ");
while (p <userlist.rows.length){
if (document.getElementsByName( "checkbox1 ")[p].checked==true){
ls_t.deleteRow(p);
p=0;
}else{
p++;
}
}
}
</script>
</head>
<body>
<input name= "add " type= "button " onclick= "AddOneLine( '未知 ') " value= "add "/>
<input name= "del " type= "button " value= "del " onclick= "clearlist() "/>
<div>
<table width= "750 " border= "1 " cellspacing= "0 " cellpadding= "0 " id= "userlist ">
<th width= "27 "> </th>
<th width= "102 "> </th>
<th width= "80 "> </th>
<th width= "67 "> </th>
<th width= "63 "> </th>
<th width= "85 "> </th>
<th width= "119 "> </th>
<th> </th>
</table>
</div>
</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> 无标题文档 </title>
</head>
<body>
<table width= "200 " border= "1 " id= "tab ">
</table>
<button onclick= "AddCheckBoxRow() "> 添加行 </button>
<button onclick= "Dele() "> 删除选中行 </button>
</body>
<script type= "text/javascript ">
var tab=document.getElementById( "tab ");
function AddCheckBoxRow(){
var cb=document.createElement( "input ");
cb.type= "checkbox ";
var tr=tab.insertRow(0);
var cell=tr.insertCell(0);
cell.appendChild(cb);
cb.Parent=tr;
tr.CB=cb;
}
function Dele(){
var i=0;
while(i <tab.rows.length){
if(tab.rows[i].CB.checked==true){
tab.deleteRow(i);
i--;
}
i++;
}
}
</script>
</html>