日期:2014-05-16  浏览次数:20359 次

关于用javascript 自动计算的问题
我做的一个考评表是用excel先设计好的   然后用JSP做     主要涉及到的自动计算有些麻烦,包括所有行的自动计算,最后还要把   最后一列的得分自动计算出来     而且有自动增加行的功能   就是每行的表单的name是一样的     所以计算最后一列的得分比较困难,下面是个样例,哪位高手帮忙解决一下,我想实现的就是先每行各自计算     最后把等于号后面的3个加起来该什么实现,因为他们的name相同


<html>
<head>
<meta   http-equiv= "Content-Type "   content= "text/html;   charset=gb2312 ">
<title> +&nbsp;   = </title>
</head>
<script   type= "text/javascript ">
function   startCalc(){
    interval   =   setInterval( "calc() ",1);
}
function   calc(){
    one   =   document.autoSumForm.firstBox.value;
    two   =   document.autoSumForm.secondBox.value;  
    document.autoSumForm.thirdBox.value   =   (one   *   1)   +   (two   *   1);
}
function   stopCalc(){
    clearInterval(interval);
}
</script>
<form   name= "autoSumForm ">
<input   type=text   name= "firstBox "   value= " "   onFocus= "startCalc(); "   onBlur= "stopCalc(); ">   +  
<input   type=text   name= "secondBox "   value= " "   onFocus= "startCalc(); "   onBlur= "stopCalc(); ">   =  
<input   type=text   name= "thirdBox "> <br>
<input   type=text   name= "firstBox "   value= " "   onFocus= "startCalc(); "   onBlur= "stopCalc(); ">   +  
<input   type=text   name= "secondBox "   value= " "   onFocus= "startCalc(); "   onBlur= "stopCalc(); ">   =  
<input   type=text   name= "thirdBox "> <br>
<input   type=text   name= "firstBox "   value= " "   onFocus= "startCalc(); "   onBlur= "stopCalc(); ">   +  
<input   type=text   name= "secondBox "   value= " "   onFocus= "startCalc(); "   onBlur= "stopCalc(); ">   =  
<input   type=text   name= "thirdBox ">
</form>
<p>   </p>
</body>
</html>


------解决方案--------------------
自动记算
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> +&nbsp; = </title>
</head>
<script type= "text/javascript ">
function startCalc(){
interval = setInterval( "calc() ",1);
}
function calc(){

fi= document.getElementsByName( "firstBox ");
//alert(fi[0].value)
se = document.getElementsByName( "secondBox ");
thi = document.getElementsByName( "thirdBox ");
for (i=0;i <fi.length;i++){
thi[i].value=((fi[i].value)*1)+((se[i].value)*1);
}
//two = document.autoSumForm.secondBox.value;
//document.autoSumForm.thirdBox.value = (one * 1) + (two * 1);
}
function stopCalc(){
clearInterval(interval);
}
</script>
<form name= "autoSumForm ">
<input type