<SCRIPT LANGUAGE=javascript>
function sumNum(){
var number1 = isNaN(parseInt(importantForm.leader.value,10))?0:parseInt(importantForm.leader.value,10);
var number2 = isNaN(parseInt(importantForm.dept.value,10))?0:parseInt(importantForm.dept.value,10);
var number3 = isNaN(parseInt(importantForm.province.value,10))?0:parseInt(importantForm.province.value,10);
var number4 = isNaN(parseInt(importantForm.stat.value,10))?0:parseInt(importantForm.stat.value,10);
importantForm.total.value = number1 + number2 + number3 + number4;
}
</SCRIPT>
<%for(int i=0;i <5;i++){%>
<tr align= "center ">
<td class= "template-td-6 "> </td>
<td class= "template-td-6 "> <input id= "leader <%=i%> " type= "text " class= "in " style= "width:100% " onpropertychange= "sumNum() "> </td>
<td class= "template-td-6 "> <input id= "dept <%=i%> " type= "text " class= "in " style= "width:100% " onpropertychange= "sumNum() "> </td>
<td class= "template-td-6 "> <input id= "province <%=i%> " type= "text " class= "in " style= "width:100% " onpropertychange= "sumNum() "> </td>
<td class= "template-td-6 "> <input id= "stat <%=i%> " type= "text " class= "in " style= "width:100% " onpropertychange= "sumNum() "> </td>
<td class= "template-td-6 "> <input id= "total1 <%=i%> " type= "text " class= "in " style= "width:100% "> </td>
</tr>
<%}%>
如题,如果是一个 <tr> ,相加好处理,上面的脚本就能实现
但是tr是多个,由for循环确定,怎样在脚本获取每一个tr中的文本框ID相加
------解决方案--------------------
<script>
function sumNum(obj){
var trObj=obj.parentNode.parentNode;
var number1 = isNaN(parseInt(trObj.childNodes[1].firstChild.value,10))?0:parseInt(trObj.childNodes[1].firstChild.value,10);
var number2 = isNaN(parseInt(trObj.childNodes[2].firstChild.value,10))?0:parseInt(trObj.childNodes[2].firstChild.value,10);
var number3 = isNaN(parseInt(trObj.childNodes[3].firstChild.value,10))?0:parseInt(trObj.childNodes[3].firstChild.value,10);
var number4 = isNaN(parseInt(trObj.childNodes[4].firstChild.value,10))?0:parseInt(trObj.childNodes[4].firstChild.value,10);
trObj.childNodes[5].firstChild.value = number1 + number2 + number3 + number4;
}
</script>
将所有onpropertychange= "sumNum() "换成onpropertychange= "sumNum(this) "