Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript .toFixed仅处理1个表单,而不是全部3个表单_Javascript - Fatal编程技术网

Javascript .toFixed仅处理1个表单,而不是全部3个表单

Javascript .toFixed仅处理1个表单,而不是全部3个表单,javascript,Javascript,我有一个我正在开发的网站,在那里我制作了一个javascript计算器,我需要将页面中的所有3个表单四舍五入到2。我习惯于在我的第一张表格上固定(2)。其他两个表单仍然显示所有小数点,即使我在它们上使用了.toFixed(2)。有什么想法吗 <script type="text/Javascript"> // Begin calculations function resetCalc() { document.forms[0].elements[1]=""; } funct

我有一个我正在开发的网站,在那里我制作了一个javascript计算器,我需要将页面中的所有3个表单四舍五入到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>

//开始计算
函数resetCalc(){
document.forms[0]。元素[1]=“”;
}
函数calcRect(){
如果(validNumber(document.getElementById('length'),'length')==true){
如果(validNumber(document.getElementById('width'),'width')==true){
变量长度=(document.getElementById('length').value)
var width=(document.getElementById('width').value)
var prodRect=(长度*宽度/9).toFixed(2)
document.getElementById('ansRect')。value=prodRect
}
}
}
函数calcCirc(){
if(validNumber(document.getElementById('diameter'),'diameter')==true){
var diameter=(document.getElementById('diameter').value)
var prodCirc=(直径/2)*(直径/2)*3.14/9.toFixed(2)
document.getElementById('ansCirc')。value=prodCirc
}
}
函数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)
变量prodTria=(基准*.5)*高度/9.toFixed(2)
document.getElementById('ansTria')。value=prodTria
}
}
}
//最终计算
//开始验证输入的值
//需要包含要计算的值的字段
函数validNumber(变量输入,字段名){
if(varInput.value==null){
警报('请为'+fieldName+'字段'输入一个值');
varInput.focus();
返回false;
}
如果(IsNumeric(VariInput.value)=false){
警报(“请在“+fieldName+”字段中仅输入数字或小数点”);
varInput.focus();
返回false;
} 
返回true;
}
//结束验证
//检查有效的数字字符串
函数为数字(strString){
var strValidChars=“0123456789”;
var-strChar;
var blnResult=真;
if(strString.length==0)返回false;
//测试字符串由上面列出的有效字符组成
对于(i=0;i
还有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>

用于矩形区域

长度 宽度 结果
对于圆形区域

圆直径 结果
对于三角形区域

基础 高度 结果

您需要像在第一个函数中一样在后两个函数中插入表达式:

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

如果没有额外的括号,在这个括号中转换的只是
9

还有,如果你把你的详细信息发给我,我会给你一张分号供应一年的优惠券。