我的脚本返回错误的值

时间:2015-12-24 15:48:58

标签: javascript html

当我为“重量”输入“100”而“高度”输入“2”时,我收到“0.0002”作为结果,这显然是错误的:

$(document).on('ready page:load', function() {
  $('#butt').click(function(){
    var hei = $("#hei").val();
    var wei = $("#wei").val();
    bmi = hei/(wei*wei);
    var result = $("#result");
    result.html("Your BMI is " + bmi);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="bmi">
  <h3>Step 1: Calculating Your Body Mass Index (BMI)</h3>
  <form>
    <label>Enter Your weight (Kg)</label>
    <input type="text" name="weight" id="wei" value="">
    <br>
    <label>Enter Your height (M):</label>
    <input type="text" name="height" id="hei" value="">
    <br>
    <button type="button" id="butt">Calculate my BMI</button>
    <p id="result">Your BMI is </p>
  </form>
</div>

2 个答案:

答案 0 :(得分:4)

问题不是脚本,而是您交换了变量;

bmi = hei/(wei*wei);

应该是

bmi = wei/(hei*hei);

答案 1 :(得分:-1)

您的计划是正确的。你的公式错了。

BMI = kg / M ^ 2

所以,你的公式应该是:

bmi = wei/(hei*hei);