为什么我代码加到form里不能用
初学者,老师们帮我看看
<form method="POST" action="?hrx=nn">
<table id='sum_test'>
<tr >
<td align="center" bgcolor="#77ADFF"><input type=text value="28" name="T1" size="20"></td>
<td align="center" bgcolor="#77ADFF"><input type="checkbox" name="hotel" value="0" onclick='sum()'></td>
</tr>
<tr >
<td >
<input type=text value="28" name="T1" size="20"></td>
<td align="center"><input type="checkbox" name="hotel" value="0" onclick='sum()'></td>
</tr>
</table>
<input type="text" id="sum" name="textfield">
<script type='text/javascript'>
function sum(){
var forScan=document.getElementById('sum_test');
var tbody;
var sumall=0;
for(var i=0;i<forScan.childNodes.length;i++){
if(forScan.childNodes[i].tagName=='TBODY')
tbody=forScan.childNodes[i];
}
if(tbody==undefined) return;
else{
for(var i=0;i<tbody.childNodes.length;i++){
var val=tbody.childNodes[i].childNodes[0].childNodes[0].value;
var val_check=tbody.childNodes[i].childNodes[1].childNodes[0].checked;
if(val_check)
sumall+=parseInt(val);
}
document.getElementById('sum').value=sumall
}
}
</script>
</form>
------解决方案--------------------tbody.childNodes[i].childNodes[1].childNodes[0].checked......;
弄得这么复杂
取文本框的值用 document.getElementById('testid').value;
如果是表格中td的值用 document.getElementById('tdid').innerText;
------解决方案--------------------
HTML code
<form method="POST" action="?hrx=nn">
<table id='sum_test'>
<tr >
<td align="center" bgcolor="#77ADFF"><input type=text value="28" name="T1" size="20"></td>
<td align="center" bgcolor="#77ADFF"><input type="checkbox" name="hotel" value="0" onclick='sum();'></td>
</tr>
<tr >
<td >
<input type=text value="28" name="T1" size="20"></td>
<td align="center"><input type="checkbox" name="hotel" value="0" onclick='sum();'></td>
</tr>
</table>
<input type="text" id="sumObj" name="textfield">
</form>
</body>
<script type='text/javascript'>
function sum(){
var fc=function(el){
var cn=null;
for(var i=0,cns=el.childNodes,il=cns.length;i<il;i++){
if(cns[i].nodeType==1){
cn=cns[i];
break;
}
}
return cn;
};
var tab=document.getElementById('sum_test');
var s=0;
for(var i=0,rows=tab.rows,il=rows.length;i<il;i++){
if(!fc(rows[i].cells[1]).checked){
continue;
}
s+=fc(rows[i].cells[0]).value*1;
}
document.getElementById('sumObj').value=s;
}
</script>