因为你用checkbox_X去命名那个checkbox控件的id,但是这个id是不变的~~~
而表格行的下标是变化的,例如表格内有3行,第一行的checkbox的id: checkbox_1
第二行的checkbox的id: checkbox_2
第三行的checkbox的id: checkbox_3你删除了第一行以后变成第一行的checkbox的id: checkbox_2
第二行的checkbox的id: checkbox_3已经对不上了,这样你怎么根据checkbox的id值去判断哪行有选中呢~?
所以是你设计的缺陷导致出错的~~~
而表格行的下标是变化的,例如表格内有3行,第一行的checkbox的id: checkbox_1
第二行的checkbox的id: checkbox_2
第三行的checkbox的id: checkbox_3你删除了第一行以后变成第一行的checkbox的id: checkbox_2
第二行的checkbox的id: checkbox_3已经对不上了,这样你怎么根据checkbox的id值去判断哪行有选中呢~?
所以是你设计的缺陷导致出错的~~~
<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>