计算总的OnClick自定义计算器

时间:2017-09-22 04:57:40

标签: javascript html

我刚刚开始学习javascript(对不起,如果我的代码很乱)能够制作自定义计算器并将其合并到HTML中。

我希望我的计算器能够通过用户的输入来计算男性/女性的心脏;但是,单击计算按钮后,我似乎无法显示该值。

该代码的工作原理是体重和公斤体重或公斤,因为如果用户以磅为单位输入公式,则公式使用公斤,然后我将其输入转换为公斤。该公式还取决于该人是女性还是男性。从下拉菜单中选择性别和单位。

任何有关使计算按钮有效的帮助都将不胜感激。

<script>
    function computeWilks(){
        var weight = document.getElementById('weight').value;
        var total = document.getElementById('total').value;
        if (document.getElementById('units').value === 'lb'){
            var weight = (weight * 0.453592)
            var total = (total * 0.453592)
        }

        if (document.getElementById('sex').value === 'female'){
            var a = 594.31747775582;
            var b = -27.23842536447;
            var c = 0.82112226871;
            var d = -0.00930733913;
            var e = 4.731582e-05;
            var f = -9.054e-08;
        }

        if (document.getElementById('sex').value === 'male'){
            var a = -216.0475144;
            var b = 16.2606339;
            var c = -0.002388645;
            var d = -0.00113732;
            var e = 7.01863e-06;
            var f = -1.291E-08;
        }
        var coeff = (500) / ((a) + (b * weight) + (c * Math.pow(weight, 2)) + (d * Math.pow(weight, 3)) + (e * Math.pow(weight, 4)) + (f * Math.pow(weight, 5)));
        var wilks = (coeff * total)
        document.getElementById('wilks').innerHTML = "Wilks = " + wilks;
</script>

<body>
    <div class="w-calc">
        <select id="sex">
            <option value="male">male</option>
            <option value="female">female</option>
        </select>
        <select id="units">
            <option value="kg">kg</option>
            <option value="lb">lb</option>
        </select>
        <p>Body Weight: <input id="weight" type="number"></p>
        <p>Total: <input id="total" type="number"></p>
        <input type="button" onClick="computeWilks()" value="Calculate"/>
        <h2 id="wilks"></h2>
    </div>
</body> 

3 个答案:

答案 0 :(得分:0)

试试这个

 var a=0,b=0,c=0,d=0,e=0,f=0;
function computeWilks(){
    var weight = document.getElementById('weight').value;
    var total = document.getElementById('total').value;
     if (document.getElementById('units').value === 'lb'){
          weight = (weight * 0.453592)
          total = (total * 0.453592)
       }
     if (document.getElementById('sex').value === 'female'){
        a = 594.31747775582;
        b = -27.23842536447;
        c = 0.82112226871;
        d = -0.00930733913;
        e = 4.731582e-05;
        f = -9.054e-08;
     }else{
      a = -216.0475144;
      b= 16.2606339;
      c= -0.002388645;
      d = -0.00113732;
      e = 7.01863e-06;
      f = -1.291E-08;
      }
        
 var coeff = (500) / ((a) + (b * weight) + (c * Math.pow(weight, 2)) + (d * Math.pow(weight, 3)) + (e * Math.pow(weight, 4)) + (f * Math.pow(weight, 5)));
 var wilks = (coeff * total);
        document.getElementById('wilks').innerHTML = "Wilks = " + wilks;
 }
<body>
    <div class="w-calc">
        <select id="sex">
            <option value="male">male</option>
            <option value="female">female</option>
        </select>
        <select id="units">
            <option value="kg">kg</option>
            <option value="lb">lb</option>
        </select>
        <p>Body Weight: <input id="weight" type="number"></p>
        <p>Total: <input id="total" type="number"></p>
        <input type="button" onClick="computeWilks()" value="Calculate"/>
        <h2 id="wilks"></h2>
    </div>
</body> 

答案 1 :(得分:0)

试试这个:

<script>
function computeWilks(){
    var weight = document.getElementById('weight').value;
    var total = document.getElementById('total').value;
    if (document.getElementById('units').value === 'lb'){
        var weight = (weight * 0.453592)
        var total = (total * 0.453592)
    }
    if (document.getElementById('sex').value === 'female'){
        var a = 594.31747775582;
        var b = -27.23842536447;
        var c = 0.82112226871;
        var d = -0.00930733913;
        var e = 4.731582e-05;
        var f = -9.054e-08;
    }
    else{
    var a = -216.0475144;
    var b = 16.2606339;
    var c = -0.002388645;
    var d = -0.00113732;
    var e = 7.01863e-06;
    var f = -1.291E-08;
    }
    var coeff = (500) / ((a) + (b * weight) + (c * Math.pow(weight, 2)) + (d * Math.pow(weight, 3)) + (e * Math.pow(weight, 4)) + (f * Math.pow(weight, 5)));
    var wilks = (coeff * total)
    document.getElementById('wilks').innerHTML = "Wilks = " + wilks;
}
</script>

<body>
<div class="w-calc">
    <select id="sex">
        <option value="male">male</option>
        <option value="female">female</option>
    </select>
    <select id="units">
        <option value="kg">kg</option>
        <option value="lb">lb</option>
    </select>
    <p>Body Weight: <input id="weight" type="number"></p>
    <p>Total: <input id="total" type="number"></p>
    <input type="button" onClick="computeWilks()" value="Calculate"/>
    <h2 id="wilks"></h2>
</div>
</body> 

答案 2 :(得分:0)

试试这个:

<script>
    function computeWilks(){
        var weight = document.getElementById('weight').value;
        var total = document.getElementById('total').value;
        var values = {
            a: { m: -216.0475144, f: 594.31747775582 },
            b: { m: 16.2606339, f: -27.23842536447 },
            c: { m: -0.002388645, f: 0.82112226871 },
            d: { m: -0.00113732, f: -0.00930733913 },
            e: { m: 7.01863e-06, f: 4.731582e-05 },
            f: { m: -1.291E-08, f: -9.054e-08 }
        }
        if (document.getElementById('units').value === 'lb'){
            var weight = (weight * 0.453592)
            var total = (total * 0.453592)
        }
        var indexx = (document.getElementById('sex').value === 'female') ? 'f' : 'm'
        var coeff = (500) / ((values.a[indexx]) + (values.b[indexx] * weight) + (values.c[indexx] * Math.pow(weight, 2)) + (values.d[indexx] * Math.pow(weight, 3)) + (values.e[indexx] * Math.pow(weight, 4)) + (values.f[indexx] * Math.pow(weight, 5)));
        var wilks = (coeff * total)
        document.getElementById('wilks').innerHTML = "Wilks = " + wilks;
    }
</script>

希望这有帮助。