.toFixed只处理1个表单而不是全部3个表单

时间:2016-07-04 21:49:34

标签: javascript

我有一个网站,我正在制作一个javascript计算器,我需要在页面中的所有3个表单舍入到2.我在我的第一个表单上使用了固定(2)。其他2个表单仍然显示所有小数点,即使我在它们上使用了.toFixed(2)。任何想法

<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 = "0123456789.";
   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>

和html

<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>

1 个答案:

答案 0 :(得分:0)

你需要像在第一个函数中那样用括号表示第二个函数中的表达式:

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

如果没有额外的括号,那个被转换的所有内容都是9