js有一选择删除表格,删除其中某行后,其合计却不相应重计或重计不正确
网友可将以下代码运行,然后输入数量,完后再在其某行前的选择框打勾后按删除(delete)键,再看合计,就明白,原合计不变或变化不正确。再请高手帮助确诊一下,问题在哪?
代码如下:
<table width="960" border="0" cellspacing="0" cellpadding="0" class="tb" id="tlist">
<tr class="tit1" align='center'>
<td width="0"><input type="checkbox" name="checkall" onClick="check_all(this)" ></td>
<td width="138"><input type= "text" id="fdesc1" name="fdesc1" size=32 value="ITEM NO"> </td>
<td width="308"><input type="text" name="fdesc1" id="fdesc1" size=32 value="ITEM DESCRIPTION"> </td>
<td width="62"><input type="text" name="fdesc1" id="fdesc1" size=7 value="QTY"> </td>
<td width="55"><input type="text" name="fdesc1" id="fdesc1" size=6 value="PRICE"> </td>
<td width="42"><input type="text" name="fdesc1" id="fdesc1" size=4 value="INNER"> </td>
<td width="42"><input type="text" name="fdesc1" id="fdesc1" size=4 value="OUTER"></td>
<td width="68"><input type="text" name="fdesc1" id="fdesc1" size=8 value="TOTAL "> </td>
<td width="210"><input type="text" name="fdesc1" id="fdesc1" size=28 value="REMARKS"> </td>
</tr>
</table>
<table width="960" border="0" cellspacing="0" cellpadding="0" class="tb" id="tlist">
<script language="JavaScript">
function check_all(obj){
for(i=0;i<document.getElementsByName("c").length;i++){
document.getElementsByName("c")[i].checked=obj.checked;
}
}
</script>
<tr class="tit1" align='center'>
<td> <input type="checkbox" name="c" /><input type= "text" id="fno1" name="fno1" size=32 value="001"> </td>
<td> <input type="text" name="fdesc1" id="fdesc1" size=32 value="television"> </td>
<td> <input type="text" id="fqty1" name="fqty1" size=7 maxlength=7 value='250' onchange='chtotal(1);'> </td>
<td> <input type="text" id="fprice1" name="fprice1" size=6 maxlength=6 value='0' onchange='chtotal(1);'> </td>
<td> <input type="text" id="finners1" name="finners1" size=4 maxlength=4> </td>
<td> <input type="text" id="fouters1" name="fouters1" size=4 maxlength=4> </td>
<td> <input type="text" id="ftotal1" name="ftotal1" size=8 readonly > </td>
<td> <input type="text" id="fremarks1" name="fremarks1" size=28 maxlength=100> </td>
</tr>
<tr class="tit2" align='center'>
<td> <input type="checkbox" name="c" /><input type=<input type= "text" id="fdesc2" name="fdesc2" size=32 value="002"/> </td>
<td> <input type="text" name="fdesc2" id="fdesc" size=32 value="radio"> </td>
<td> <input type="text" id="fqty2" name="fqty2" size=7 maxlength=7 value='150' onchange='chtotal(2);'> </td>
<td> <input type="text" id="fprice2" name="fprice2" size=6 maxlength=6 value='0' onchange='chtotal(2);'> </td>
<td> <input type="text" id="finners2" name="finners2" size=4 maxlength=4> </td>
<td> <input type="text" id="fouters2" name="fouters2" size=4 maxlength=4> </td>
<td> <input type="text" id="ftotal2" name="ftotal2" size=8 readonly > </td>
<td> <input type="text" id="fremarks2" name="fremarks2" size=28 maxlength=100> </td>
</tr>
<tr class="totalsum"><td>Total</td><td colspan=4></td><td colspan=2 align='right'><b id="totalsum">0</b></td><td colspan=2></td></tr>
<script language="javascript">
function deleteAll(obj){
var checked = document.getElementsByName(obj);
debugger
for(var i = 0; i < checked.length; i ++){
if(checked[i].checked){
var tr=checked[i].parentNode.parentNode;
var tbody=tr.parentNode;
tbody.removeChild(tr);
i--;
}
}
}
</script>
<td><input type="button" name="delete_button" value="Delete" onclick="deleteAll('c');"></td>
</table>
<script type="text/javascript">
function delrecord(btn) {/////////
var tr = btn.parentNode.parentNode, tbody = tr.parentNode, rowIndex = tr.rowIndex+1;
var t = document.getElementById("ftotal" + rowIndex), tt = document.getElementById("totalsum");
var btn = document.getElementById('delbutton' + (rowIndex - 1));
if (btn) btn.disabled = false; //这里将上一个button设置为有效
tbody.removeChild(tr);
}
function chtotal(id) {
var tt = document.getElementById("totalsum");
var q = document.getElementById("fqty" + id);
var p = document.getElementById("fprice" + id);
var t = document.getElementById("ftotal" + id);
if (q.value != '' && p.value != '') {
var old = t.getAttribute('ov');
if (old) old = parseInt(old); else old = 0;
t.value = q.value * p.value;
t.setAttribute('ov', t.value);
tt.innerHTML = parseInt(tt.innerHTML) + parseInt(t.value) - old;
}
}
</script>
function deleteAll(obj){
var checked = document.getElementsByName(obj);
debugger
for(var i = 0; i < checked.length; i ++){
if(checked[i].checked){
var tr=checked[i].parentNode.parentNode;
var tbody=tr.parentNode;
document.getElementById("fprice" + (i+1)).value=0;
chtotal(i+1);
tbody.removeChild(tr);
}
}
}