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

关于javascript自动计算的问题
我做的一个表包含有自动计算,就是左边的值改变后,右边等号后面的值根据左边的值自动计算,这个功能实现了,但是右边表单计算必须在左边表单点击后才能计算出值,比如左边的第一行的第一个表单的值是5,当点击这个表单后等号后面自动计算出是5
,可不可以不点就得出值,就好比直接打开网业,右边的值就根据左边自动计算,请高手们帮忙解决.
代码如下:
<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(){
    //alert( "yy ");
    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);
}
function   app(){
    o=document.getElementById( "aa ");
    str=o.innerHTML;
    //alert(str);
    o1=document.getElementById( "bb ");  
    //alert(o1.html);
    o1.innerHTML=   o1.innerHTML+ " </br> "+str;
}

function   sum(){//加总
    thi   =   document.getElementsByName( "thirdBox ");
    tot   =   document.getElementsByName( 'totalBox ');
    tot[0].value= "0 ";
    for(i=0;i <thi.length;i++){
        if(thi[i].value!= ' '){
            tot[0].value=tot[0].value*1+thi[i].value*1;
}
    }
}
</script>
<form   name= "autoSumForm "   id= "form1 ">
<div   id= "aa "> <input   type=text   name= "firstBox "   value= "5 "   onFocus= "startCalc(); "   onBlur= "stopCalc();sum(); ">   +  
<input   type=text   name= "secondBox "   value= " "   onFocus= "startCalc(); "   onBlur= "stopCalc();sum(); ">   =  
<input   type=text   name= "thirdBox "> <br> </div>
<input   type=text   name= "firstBox "   value= " "   onFocus= "startCalc(); "   onBlur= "stopCalc();sum(); ">   +  
<input   type=text   name= "secondBox "   value= " "   onFocus= "startCalc(); "   onBlur= "stopCalc();sum(); ">   =  
<input   type=text   name= "thirdBox "> <br>
<div   id= "bb "> <input   type=text   name= "firstBox "   value= " "   onFocus= "startCalc(); "   onBlur= "stopCalc();sum(); ">   +  
<input   type=text   name= "sec