2014-02-18 32 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