我无法点击运行我的功能?

时间:2016-07-05 01:57:39

标签: javascript html

<!DOCTYPE html public "-//w3c//dtd html 3.2 final//en">
<html>
<head>

    <title>Check your BMI</title>

    <meta name="author" content="me">
    <meta name="keywords" content="?">
    <meta name="description" content="JavaScript example - Calculate the `enter code here`BMI">

    <script type="text/javascript">
    <!-- Hide the script from old browsers

       function calcBmi()
       {
          var feet = parseFloat(document.getElementById("feet").value);
          var inches = parseFloat(document.getElementById("inches").value);
          var weight = parseFloat(document.getElementById("weight").value);

          var height = (feet * 12) + inches;
          var bmi = (weight * 703)/(Math.pow(height, 2));


          var messageTag = document.getElementById("message");

          if (bmi < 18.5){
          messageTag.innerHTML = "Your BMI is + bmi + . You're under weight!! You need a BMI of between 18.5 - 24.9 to be within normal range. Eat some chocolate pudding!!"
          }else if (bmi >= 18.5) && (bmi <= 24.9){
          messageTag.innerHTML = "Your BMI is + bmi + . What a specimen of a human being! You are the perfect weight! Your BMI is within normal range - 18.5 - 24.9."
          }else if (bmi >= 25) && (bmi <= 29.9){
          messageTag.innerHTML = "Your BMI is + bmi + . Go easy on the cheeseburgers!! You are a bit over weight! You need a BMI of between 18.5 - 24.9 to be within normal range."
          }else {
          messageTag.innerHTML = "Your BMI is + bmi + . Your obese. This carries a lot health risks. You need a BMI of between 18.5 - 24.9 to be within normal range."
          }
      }

      // end hiding -->
    </script>

  </head>

  <body>

     <h1>Check Your BMI</h1>

     <p>
       Gearing up for a hot date? Dieting for that glamorous wedding? Check your BMI! See what shape you're in!!
     </p>

         <form name="bmiForm">
         Height:
         <input type="text" id="feet" value="" />
         ft
         <input type="text" id="inches" value="" />
         Inches<br />
         <p>Weight:
         <input type="text" id="weight" value="" /></p><br />
         <p><span id="message"></span></p>
         <input type="button" value="Calculate" onclick="calcBmi()" />
         <input type="reset" value="Reset" id="reset" /><br />
         </form>



  </body>
</html>

2 个答案:

答案 0 :(得分:2)

函数中的两个else if行有语法错误。整个条件需要括在括号中,因此您需要更改:

}else if (bmi >= 18.5) && (bmi <= 24.9){

}else if (bmi >= 18.5 && bmi <= 24.9){

或者如果您想在每个>=<=测试周围保留括号,您可以执行以下操作(但这些都不是必需的):

}else if ((bmi >= 18.5) && (bmi <= 24.9)){

对其他else if进行相同的更改。

您尝试显示计算值的方式也有错误(不是语法错误)。你需要改变:

"Your BMI is + bmi + . You're..."

...其中包含字符串中的文字字符+ bmi +

"Your BMI is " + bmi + ". You're..."

...以便变量 bmi与字符串文字连接。

在上下文中:

&#13;
&#13;
  function calcBmi() {
    var feet = parseFloat(document.getElementById("feet").value);
    var inches = parseFloat(document.getElementById("inches").value);
    var weight = parseFloat(document.getElementById("weight").value);

    var height = (feet * 12) + inches;
    var bmi = (weight * 703) / (Math.pow(height, 2));

    var messageTag = document.getElementById("message");

    if (bmi < 18.5) {
      messageTag.innerHTML = "Your BMI is " + bmi + ". You're under weight!! You need a BMI of between 18.5 - 24.9 to be within normal range. Eat some chocolate pudding!!"
    } else if (bmi >= 18.5 && bmi <= 24.9) {
      messageTag.innerHTML = "Your BMI is " + bmi + ". What a specimen of a human being! You are the perfect weight! Your BMI is within normal range - 18.5 - 24.9."
    } else if (bmi >= 25 && bmi <= 29.9) {
      messageTag.innerHTML = "Your BMI is " + bmi + ". Go easy on the cheeseburgers!! You are a bit over weight! You need a BMI of between 18.5 - 24.9 to be within normal range."
    } else {
      messageTag.innerHTML = "Your BMI is " + bmi + ". Your obese. This carries a lot health risks. You need a BMI of between 18.5 - 24.9 to be within normal range."
    }
  }
&#13;
Height:
<input type="text" id="feet" value="" />ft
<input type="text" id="inches" value="" />Inches
<br />
<p>Weight:
  <input type="text" id="weight" value="" />
</p>
<br />
<p><span id="message"></span>
</p>
<input type="button" value="Calculate" onclick="calcBmi()" />
&#13;
&#13;
&#13;

答案 1 :(得分:0)

function calcBmi()
       {
          var feet = parseFloat(document.getElementById("feet").value);
          var inches = parseFloat(document.getElementById("inches").value);
          var weight = parseFloat(document.getElementById("weight").value);

          var height = (feet * 12) + inches;
          var bmi = (weight * 703)/(Math.pow(height, 2));


    var messageTag = document.getElementById("message");

          if (bmi < 18.5){
          messageTag.innerHTML = "Your BMI is" + bmi + ". You're under weight!! You need a BMI of between 18.5 - 24.9 to be within normal range. Eat some chocolate pudding!!"
          }else if (bmi >= 18.5 && bmi <= 24.9){
          messageTag.innerHTML = "Your BMI is" + bmi +" . What a specimen of a human being! You are the perfect weight! Your BMI is within normal range - 18.5 - 24.9."
          }else if (bmi >= 25 && bmi <= 29.9){
          messageTag.innerHTML = "Your BMI is" + bmi + ". Go easy on the cheeseburgers!! You are a bit over weight! You need a BMI of between 18.5 - 24.9 to be within normal range."
          }else {
          messageTag.innerHTML = "Your BMI is" + bmi + ". Your obese. This carries a lot health risks. You need a BMI of between 18.5 - 24.9 to be within normal range."
          }
      }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h1>Check Your BMI</h1>

     <p>
       Gearing up for a hot date? Dieting for that glamorous wedding? Check your BMI! See what shape you're in!!
     </p>

         <form name="bmiForm">
         Height:
         <input type="text" id="feet" value="" />
         ft
         <input type="text" id="inches" value="" />
         Inches<br />
         <p>Weight:
         <input type="text" id="weight" value="" /></p><br />
         <p><div id="message"></div></p>
         <input type="button" onclick="calcBmi()" value="Calculate" onclick="calcBmi()" />
         <input type="reset" value="Reset" id="reset" /><br />
         </form>