2016-07-04 2 views
-1

나는 JavaScript 계산기를 만든 곳에서 작업하고 있으며, 페이지에서 3을 모두 사용해야합니다. toFixed (2) 내 첫 번째 양식에. .toFixed (2)를 사용해도 다른 두 형식은 여전히 ​​소수점을 표시합니다. 어떤 아이디어.toFixed는 하나의 양식에서만 작동하며 모두 3이 아닙니다.

<script type="text/Javascript">  
// Begin calculations 
function resetCalc() { 
document.forms[0].elements[1]=""; 
} 

function calcRect() { 
if(validNumber(document.getElementById('length'), 'length') == true) {  
    if(validNumber(document.getElementById('width'), 'width') == true) { 
     var length = (document.getElementById('length').value) 
     var width = (document.getElementById('width').value) 

     var prodRect = (length * width/9) .toFixed(2) 

     document.getElementById('ansRect').value = prodRect 
     } 
    } 
} 
function calcCirc() { 
if(validNumber(document.getElementById('diameter'), 'diameter') == true) { 
    var diameter = (document.getElementById('diameter').value) 

    var prodCirc = (diameter/2) * (diameter/2) * 3.14 /9 .toFixed(2) 
    document.getElementById('ansCirc').value = prodCirc 
    } 
} 

function calcTria() { 
if(validNumber(document.getElementById('base'), 'base') == true) { 
    if(validNumber(document.getElementById('height'), 'height') == true) { 
     var base = (document.getElementById('base').value) 
     var height = (document.getElementById('height').value) 

     var prodTria = (base * .5) * height/9 .toFixed(2) 
     document.getElementById('ansTria').value = prodTria 
     } 
    } 
} 
// End Calculations 

// BEGIN Validate Values entered 
//Requires field that contians value being evaluated 
function validNumber(varInput, fieldName) { 

if (varInput.value == null) { 
     alert('Please enter a value for the ' + fieldName + ' field'); 
     varInput.focus(); 
     return false; 
    } 

if (IsNumeric(varInput.value) == false) { 
     alert('Please enter only numbers or decimal points in the ' + fieldName + ' field'); 
     varInput.focus(); 
     return false; 
    } 

return true; 
} 
// END Validation 

// check for valid numeric strings  
function IsNumeric(strString) { 
    var strValidChars = "."; 
    var strChar; 
    var blnResult = true; 

    if (strString.length == 0) return false; 

    // test strString consists of valid characters listed above 
    for (i = 0; i < strString.length && blnResult == true; i++) 
     { 
     strChar = strString.charAt(i); 
     if (strValidChars.indexOf(strChar) == -1) 
     { 
     blnResult = false; 
     } 
     } 

    return blnResult; 
} 
// 
</script> 

그리고

당신은 당신이 처음에했던 것처럼 두 번째 두 함수의 식을 괄호로 할 필요가
<form name="sodCalc" id="formEnquire" action=""> 
    <div id="ctl00_body_sodCalc1_validationSummarySodCalc" style="color:Red;display:none;"> </div> 
    <p><strong>For a rectangular shaped area</strong></p> 
    <p> 
    <label>Length</label> 
    <input name="length" id="length" type="text" tabindex="1" /> 
    <label>Width</label> 
    <input name="width" id="width" type="text" tabindex="1" /> 
    <label>Result</label> 
    <input type="text" value="0" name="ansRect" id="ansRect" /> 
    <br class="clear"/> 
    <input type="button" value="Calculate" onclick="calcRect()" name="btnRect" id="btnRect" /> 
    <input type="reset" value="Reset" onclick="resetCalc()" name="reset" id="reset" /> 
    </p> 
</form> 
<form name="sodCalc" id="formEnquire" action=""> 
    <div id="ctl00_body_sodCalc1_validationSummarySodCalc" style="color:Red;display:none;"> </div> 
    <p><strong>For a circle area </strong></p> 
    <p> 
    <label>Diameter Of Circle</label> 
    <input name="diameter" id="diameter" type="text" tabindex="1" /> 
    <label>Result</label> 
    <input type="text" size="6" value="0" name="ansCirc" id="ansCirc" /> 
    <br class="clear"/> 
    <input type="button" value="Calculate" onclick="calcCirc()" name="btnCirc" id="btnCirc" /> 
    <input type="reset" value="Reset" onclick="resetCalc()" name="reset" id="reset" /> 
` </p> 
</form> 
<form name="sodCalc" id="formEnquire" action=""> 
    <div id="ctl00_body_sodCalc1_validationSummarySodCalc" style="color:Red;display:none;"> </div> 
    <p><strong>For a triangle area </strong></p> 
    <p> 
    <label>Base</label> 
    <input name="base" id="base" type="text" tabindex="1" /> 
    <label>Height</label> 
    <input name="height" id="height" type="text" tabindex="1" /> 
    <label>Result</label> 
    <input type="text" size="6" value="0" name="ansTria" id="ansTria" /> 
    <br class="clear"/> 
    <input type="button" value="Calculate" onclick="calcTria()" name="btnTria" id="btnTria" /> 
    <input type="reset" value="Reset" onclick="resetCalc()" name="reset" id="reset" /> 
    </p> 
</form> 

답변

0

html로 : 여분의 괄호없이

var prodTria = ((base * .5) * height/9).toFixed(2) 

, 저것 안에 개조되고 있던 모두는 9이었다.

+2

또한 귀하가 세부 정보를 보내 주시면 1 년 동안 세미콜론을 제공 할 수있는 쿠폰을드립니다. – Pointy

관련 문제