2012-02-28 18 views
-1

您好我正在做JavaScript中的autosum,這工作正常在這裏的鏈接http://maatren.com/auto-sum.html但我也想反向autosum。javascript autosum

兩者工作正常,但分開。他們在一起工作不好。

我使用此代碼

function startCalc(){ 
     interval = setInterval("calc()",1); 
    } 
    function calc(){ 
     one = document.autoSumForm.firstBox.value; 
     two = document.autoSumForm.secondBox.value; 
    third = document.autoSumForm.thirdBox.value; 
     document.autoSumForm.thirdBox.value = (one * 1) + ((two/100) * one); 
    //document.autoSumForm.firstBox.value = (third * 1) - ((two/100) * third); 
} 
function stopCalc(){ 
    clearInterval(interval); 
} 

我的HTML是

<div style="width: 200px; text-align: center;"> 
    <form name="autoSumForm"> 
     <input class="right" type=text name="firstBox" value="" onFocus="startCalc();" onBlur="stopCalc();"><br> 
     % <input class="right" type=text name="secondBox" value="10" onFocus="startCalc();" onBlur="stopCalc();"><br> 
     = <input class="right" type=text name="thirdBox" value="" onFocus="startCalc();" onBlur="stopCalc();"><br> 
    </form> 
</div> 

的問題是,當我刪除第三框註釋然後上衣工作。我同時需要autosum和reverse autosum。

+2

在繼續提出更多問題之前,您應該接受一些問題的答案。 – 2012-02-28 07:37:40

回答

0
<div style="width: 200px; text-align: center;"> 
    <form name="autoSumForm"> 
     <input class="right" type=text name="firstBox" value="" onkeyup="calc(1);"><br> 
     % <input class="right" type=text name="secondBox" value="10" onkeyup="calc(2);"><br> 
     = <input class="right" type=text name="thirdBox" value="" onkeyup="calc(3);"><br> 
    </form> 
</div> 
​ 

function calc(box){ 
    one = document.autoSumForm.firstBox.value; 
    two = document.autoSumForm.secondBox.value; 
    third = document.autoSumForm.thirdBox.value; 

    if(box == 1){ 
     document.autoSumForm.thirdBox.value = (one * 1) + ((two/100) * one);; 
    }else if(box == 2 || box == 3){ 
     document.autoSumForm.firstBox.value = (third * 1) - ((two/100) * third); 
    } 

}