2014-02-18 39 views
1

行,所以我需要修改它低于以能够识别用户是在哪个类别的代码的分类是: 代码修改,以确定类别中的HTML代码

  • 小于18.5体重不足
  • 18.5至25正常
  • 25-30超重
  • 超过30肥胖

,然后在结束我需要的代码automaticall y知道用户在什么类别并显示:这意味着你是(低体重,正常等)

谢谢。

<html> 
<head> 
    <title>BMI Calculator</title> 
    <script type="text/javascript"> 
     function computeBMI() 
     { 
      //Obtain user inputs 
      var height=Number(document.getElementById("height").value); 
      varheightunits=document.getElementById("heightunits").value; 
      var weight=Number(document.getElementById("weight").value); 
      varweightunits=document.getElementById("weightunits").value; 

      //Convert all units to metric 
      if (heightunits=="inches") height/=39.3700787; 
      if (weightunits=="lb") weight/=2.20462; 

      //Perform calculation 
      var BMI=weight/Math.pow(height,2); 

      //Display result of calculation 
      document.getElementById("output").innerText=Math.round(BMI*100)/100; 
     } 
    </script> 
</head> 
<body> 
    <h1>Body Mass Index Calculator</h1> 
    <p>Enter your height: <input type="text" id="height"/> 
     <select type="multiple" id="heightunits"> 
      <option value="metres" selected="selected">metres</option> 
      <option value="inches">inches</option> 
     </select> 
    </p> 
    <p>Enter your weight: <input type="text" id="weight"/> 
     <select type="multiple" id="weightunits"> 
      <option value="kg" selected="selected">kilograms</option> 
      <option value="lb">pounds</option> 
     </select> 
    </p> 
    <input type="submit" value="computeBMI" onclick="computeBMI();"> 
    <h1>Your BMI is: <span id="output">?</span></h1> 
</body> 
</html> 
+4

为什么我觉得我做的某人的学校作业? – Schien

+0

哈哈,那么,你是:P – witherwind

+0

好吧,有一些语法错误...'varheightunits'可能是'var heightunits' – helderdarocha

回答

0

在你的HTML添加这一行,也许下面的<h1>标签:

<span id="id_here"></span> 

任何其他元素将工作,但只要有一个id。

然后在你的JS,你补充一点:

var writeThere = document.getElementById("id_of_the_element_below_h1"); 

if(ans < 18.5) { 
    writeThere.innerText= "You are underweight"; 
} else if(ans > 18.5 && ans < 25) { 
    writeThere.innerText= "You are normal"; 
} else if(ans > 25 && ans < 30) { 
    writeThere.innerText= "You are overweight"; 
} else { 
    writeThere.innerText= "You are obese"; 
} 

你可能会问哪来的变量,也可以是在这里:

var ans = Math.round(BMI*100)/100; 
//Display result of calculation 
document.getElementById("output").innerText= ans; 
+0

你需要改变你的代码,因为如果某人恰好是18.5或25,他们就会肥胖。您可以移除每个测试的大部分,因为这些是多余的,因为'if/else if'将捕获较低的值。例如,如果(ans <25)。这将解决问题。 –

+0

我完全了解,但这是提问者想要的,所以我会让它滑动。 – witherwind