请教各位(怎么删除的行数不对)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN "
"http://www.w3.org/TR/html4/loose.dtd ">
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> DELETE </title>
</head>
<script language= "javascript ">
function DeleteRowTest(rowid){
alert(rowid);
document.all.ObjTable.deleteRow(rowid);
document.all.ObjTable.update();
}
function AddRow(){
for(var i=0;i <10;i++){
var newRow = document.all.ObjTable.insertRow();
var newTD = newRow.insertCell(0);
var newTD1 = newRow.insertCell(1);
var newTD2 = newRow.insertCell(2);
newTD.innerHTML=i;
newTD1.innerHTML= " <input type= 'button ' class= 'bottom1 ' onclick=DeleteRowTest( "+newRow.rowIndex+ ") value= '接受 '> ";
newTD2.innerHTML= " <input type= 'button ' class= 'bottom1 ' onclick=DeleteRowTest( "+newRow.rowIndex+ ") value= '拒绝 '> ";
}
}
</script>
<body onLoad= "AddRow() ">
<table width= "200 " border= "1 " id= "ObjTable ">
</table>
</body>
</html>
[解决办法]
因为你rowIndex是固定的,删除一行之后,表格的rowIndex会发生变化,而你的函数里面是定死的。换成下面即可
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN "
"http://www.w3.org/TR/html4/loose.dtd ">
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> DELETE </title>
</head>
<script language= "javascript ">
function DeleteRowTest(o){
obj = o.parentNode
while(obj.tagName!= "TR ")
obj = obj.parentNode
if(obj.tagName!= "TR ") return
obj.parentNode.removeChild(obj);
}
function AddRow(){
for(var i=0;i <10;i++){
var newRow = document.all.ObjTable.insertRow();
var newTD = newRow.insertCell(0);
var newTD1 = newRow.insertCell(1);
var newTD2 = newRow.insertCell(2);
newTD.innerHTML=i;
newTD1.innerHTML= " <input type= 'button ' class= 'bottom1 ' onclick=DeleteRowTest(this) value= '接受 '> ";
newTD2.innerHTML= " <input type= 'button ' class= 'bottom1 ' onclick=DeleteRowTest(this) value= '拒绝 '> ";
}
}
</script>
<body onLoad= "AddRow() ">
<table width= "200 " border= "1 " id= "ObjTable ">
</table>
</body>
</html>
[解决办法]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN "
"http://www.w3.org/TR/html4/loose.dtd ">
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> DELETE </title>
</head>
<script language= "javascript ">
function DeleteRowTest(rowid){
var tr = document.getElementById(rowid);
document.all.ObjTable.deleteRow(tr.rowIndex);
//document.all.ObjTable.update();
}
function AddRow(){
for(var i=0;i <10;i++){
var newRow = document.all.ObjTable.insertRow();
var newTD = newRow.insertCell(0);
var newTD1 = newRow.insertCell(1);
var newTD2 = newRow.insertCell(2);
newRow.id= "tr_ " + i;
newTD.innerHTML=i;
newTD1.innerHTML= " <input type= 'button ' class= 'bottom1 ' onclick=DeleteRowTest( ' "+newRow.id+ " ') value= '接受 '> ";
newTD2.innerHTML= " <input type= 'button ' class= 'bottom1 ' onclick=DeleteRowTest( ' "+newRow.id+ " ') value= '拒绝 '> ";
}
}
</script>
<body onLoad= "AddRow() ">
<table width= "200 " border= "1 " id= "ObjTable ">
</table>
</body>
</html>