首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

关于javascript自动计算的有关问题

2012-02-19 
关于javascript自动计算的问题我做的一个表包含有自动计算,就是左边的值改变后,右边等号后面的值根据左边

关于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= "secondBox "   value= " "   onFocus= "startCalc(); "   onBlur= "stopCalc();sum(); ">   =  


<input   type=text   name= "thirdBox "> </div>
<input   type= "button "   value= "append "   onclick= "app();sum() "> <span   style= "margin-left:262px "> sum: <input   type= "text "   name= "totalBox "   value= "0 "> </span>
</form>
<p>   </p>
</body>
</html>

[解决办法]
window.onload = function(){计算语句}
[解决办法]
这样好了呀:
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> +&nbsp; = </title>
</head>
<script type= "text/javascript ">
window.onload = 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= "secondBox " value= " " onFocus= "startCalc(); " onBlur= "stopCalc();sum(); "> =
<input type=text name= "thirdBox "> </div>
<input type= "button " value= "append " onclick= "app();sum() "> <span style= "margin-left:262px "> sum: <input type= "text " name= "totalBox " value= "0 "> </span>
</form>
<p>   </p>


</body>
</html>

[解决办法]
window.onload = function startCalc(){
interval = setInterval( "calc() ",1); //上面的代码要先通过这个方法算出第三文本框的值
interval2 = setInterval( "sum() ",1) //sum()函数只是计算所有第三文本框的总和
 //这样才能得到你想要的结果
}
[解决办法]
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> +&nbsp; = </title>
</head>
<script type= "text/javascript ">
window.onload = function startCalc(){
interval = setInterval( "calc();sum(); ",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= "secondBox " value= " " onFocus= "startCalc(); " onBlur= "stopCalc();sum(); "> =
<input type=text name= "thirdBox "> </div>
<input type= "button " value= "append " onclick= "app();sum() "> <span style= "margin-left:262px "> sum: <input type= "text " name= "totalBox " value= "0 "> </span>
</form>
<p>   </p>
</body>
</html>

热点排行