使用Javascript制作计算器

时间:2017-12-24 04:37:43

标签: javascript html forms function calculator

美好的一天 - 我正在学习Javascript,我正在尝试创建一个计算器来计算安培转以磁化工具(它与我的工作有关。)我正在尝试使用一些公式来计算这个安培-转。代码对我来说似乎很好,但它不起作用。我在表单中添加了一些值,然后单击按钮提交,但没有找到结果,我也不知道为什么会发生这种情况。

我在这里分享我的代码以供您审核。并帮我解决这个问题。 谢谢。



function ampereturn()
{
    
    var inputOD = Number(document.ampereturn.inputod.value);
    var inputLen = Number(document.ampereturn.inputlen.value);
    var InputID = Number(document.ampereturn.Inputid.value);
    var InputTurn = Number(document.ampereturn.Inputturn.value);
    var ans;
  
    
    var ldratio = inputLen/inputOD;
    var coilradius = InputID/2;
    var toolradius = inputOD/2;
    var pi = 3.14;
    
    var xcoil = (coilradius * coilradius) * pi;
    var xtool = (toolradius * toolradius) * pi;
    
    var factor = xtool/xcoil;
    var text = "Use Intermediate Fill-factor formula:";
    
    if(factor >= 0.5)
        {
            ans = 35000/(ldratio+2)*Inputturn;
            document.getElementById('sum').innerHTML = ans;
        }
    
    if(factor <= 0.1)
        {
            ans = 45000/ldratio*Inputturn;
            document.getElementById('sum').innerHTML = ans;
        }
    else
        {
            document.getElementById('sum').innerHTML = text;
        }
} 
&#13;
<form name="ampereturn">
  
  <div class="w3-half w3-margin-top">
    <label>Tool OD:</label>
    <input id="inputod" class="w3-input w3-border" type="number" placeholder="Input Tool Outer Dia:">
  </div>
        
  <div class="w3-half w3-margin-top">
    <label>Tool Lenght:</label>
    <input id="inputlen" class="w3-input w3-border" type="number" placeholder="Input Tool Length">
  </div>
                            
                            
  <div class="w3-half w3-margin-top">
    <label>Coil ID:</label>
    <input id="Inputid" class="w3-input w3-border" type="number" placeholder="Input Coil Internal Dia:">
  </div>
                            
  <div class="w3-half w3-margin-top">
    <label>Coil Turn:</label>
    <input id="Inputturn" class="w3-input w3-border" type="number" placeholder="Input Number of turn in coil:">
  </div>
                            
 <div class="w3-half w3-margin-top">
    <label>Required Ampere:</label>
   <p id="sum"></p>
  </div>
                   
<button type="button" onclick="ampereturn()">Submit</button>   
</form>
<br><hr>
&#13;
&#13;
&#13;

提前谢谢你。 ....

1 个答案:

答案 0 :(得分:2)

  • 更改与表单名称相同的函数名称。
  • 在if条件下将Inputturn更改为InputTurn。

必填代码:

function Ampereturn()
{
var inputOD = Number(document.ampereturn.inputod.value);
var inputLen = Number(document.ampereturn.inputlen.value);
var InputID = Number(document.ampereturn.Inputid.value);
var InputTurn = Number(document.ampereturn.Inputturn.value);
var ans;
var ldratio = inputLen/inputOD;
var coilradius = InputID/2;
var toolradius = inputOD/2;
var pi = 3.14;   
var xcoil = (coilradius * coilradius) * pi;
var xtool = (toolradius * toolradius) * pi;
var factor = xtool/xcoil;
var text = "Use Intermediate Fill-factor formula:";
if(factor >= 0.5)
    {
        ans = 35000/(ldratio+2)*InputTurn;
        document.getElementById('sum').innerHTML = ans;
    }
 else if(factor <=0.1)
    {
        ans = 45000/ldratio*InputTurn;
        document.getElementById('sum').innerHTML = ans;
    }
else
    {
        document.getElementById('sum').innerHTML = text;
    }
}