2016-12-06 642 views
-2

我目前正在創建一個可以用javascript計算bmi的程序。我不知道爲什麼,但它不能正常工作。我一定錯過了一些東西,但我不確定它是什麼。如果有人能幫助我,我會非常感激。謝謝。用javascript計算bmi

<!DOCTYPE html> 
    <!-- --> 
<html> 

    <head> 
     <meta charset="UTF-8" /> 
     <title>Body Mass Index</title> 
    </head> 


    <BODY> 
    <header><img src="bmi.jpeg" width="380" height="132" border="0" alt="bmi"></header> 

<video controls="controls" 
width="320px" height="260px"> 
<source src="bmi.mp4"/> 

     <p>When it comes to weight loss, there's no lack of fad diets promising fast results. But such diets limit your nutritional intake, can be unhealthy, and tend to fail in the long run.</p> 

     <p>The key to achieving and maintaining a healthy weight isn't about short-term dietary changes. It's about a lifestyle that includes healthy eating, regular physical activity, and balancing the number of calories you consume with the number of calories your body uses.</p> 

     <p>BMI is a number calculated from a person's weight and height. BMI provides a reliable indicator of body fatness for most people and is used to screen for weight categories that may lead to health problems.</p> 

     <p>The BMI ranges are based on the relationship between body weight and disease and death. 
     Overweight and obese individuals are at increased risk for many diseases and health conditions, including the following:</p> 





You need a flash player to view this video. 

</video> 

     <ul> 
     <li>Hypertension</li> 
     <li>Dyslipidemia (for example, high LDL cholesterol, low HDL cholesterol, or high levels of triglycerides)</li> 
     <li>Type 2 diabetes</li> 
     <li>Coronary heart disease</li> 
     <li>Stroke</li> 
     <li>Gallbladder disease</li> 
     <li>Osteoarthritis</li> 
     <li>Sleep apnea and respiratory problems</li> 
     <li>Some cancers (endometrial, breast, and colon)</li> 
    </ul> 

<script type="text/javascript"> 
function CalculateBMI(){ 
var inch=12; 
var ft; 
var bmi= Math.write(weight*703)/ (inch height)^2; 

if(bmi<=19) 
{"Underweight"; 
} 
if else(19<bmi<=25) 
{"Desirable"; 
} 
if else(25<bmi<=29) 
{"Prone to health risks"; 
} 
if else (29<bmi<=40) 
{"obese" 
} 
else(40<bmi) 
{"Extremely Obese" 
} 
} 
</script> 
    <form name="bmi"> 


    <p> Weight:<p/></td> <td><input type="text" id="weight" name="weight" size="25" /> 


<p>Height:</p> <input type="text" id="textbox" name="textbox" size="25" /><td><p>Ft.</p><input type="text" id="textbox" name="textbox" size="25" /> <p>In.</p> 

<input type="submit" id="Calculate BMI" name="Calculate BMI" value="Calculate BMI" size="25" onclick="CalculateBMI()" /> 
According to the Panel on 
Energy, Obesity, and Body Weigth Standards published by 
American Journal of Clinical Nurttrition, your category is: 
<input type="text" id="textbox" name="textbox" size="25" /> 


    </form> 

    </BODY> 

</html> 
+0

對不起,但代碼中充滿了錯誤。從[打開瀏覽器控制檯]開始(http://webmasters.stackexchange.com/questions/8525/how-to-open-the-javascript-console-in-different-browsers)並首先計算出語法錯誤。 – JJJ

回答

1

我看到一些可能導致您的問題的事情。讓我們開始這樣的說法:

var bmi= Math.write(weight*703)/(inch height)^2; 

您沒有定義體重或身高(你必須告訴它在文本框中查看或將其發送給它不會自動知道你指的是一個文本框的功能)。我希望像

var weight = document.getElementById('weight').value; 

有身高和體重之間沒有符號,它是拋出一個語法錯誤,你需要做的這些東西,如果他們要在一起(並知道這是不添加英寸僅以英寸計算英尺)。

var bmi =(weight * 703)/(inch * height)^ 2;

這之後,你正在使用的if else - 這是不是在Javascript有效,你會想說:

else if (19<bmi<=25) 

最後你沒有返回值,也不指定其中的值應該去。

var results; 
if (bmi<=19) 
{ 
    results = "Underweight" 
} 

document.getElementById('results').value = results; 

嘗試實施其中的一些建議,看看是否讓您走上正軌。

0

您的JavaScript有幾個系統誤差

例如1<a<12是無效的,應當書面a>1 && a<12

也,你CalculateBMI功能根本不知道什麼英寸體重身高或高度英尺的高度。您可以包含JQuery以使這更容易,但document.getElementById也適用。爲了做到這一點,我還給了你的表單變量有意義的名字。

您沒有在任何地方顯示結果字符串。使用getElementById,您還可以在html中找到結果元素,並將其值設置爲計算結果。

如果您不希望在提交時刷新頁面,則必須將表單設置爲接收falseonsubmit

你的數學語法是關閉的,我假設它應該是(703 *重量)(高度)^ 2高度以英寸爲單位(在腳和英寸高度變量合併之後)。

清理過的Javascript應該看起來像這樣。請注意,這可能不是解決此問題的最佳方法。

編輯:我認爲你的BMI計算也是關閉的。如果輸入的重量和高度是英制(即英制和英制),那麼英制應乘以0.025得到米,磅應乘以0.45得到公斤。

bmi= (weight_e* 0.45)/(((12*heightf_e)+heighti_e) * 0.025)^2;

function CalculateBMI(){ 
    // To avoid erros due to hoisting define all 
    // of your vars at the top of your function  
    var ft, 
    bmi, 
    heighti_e, // height in inches 
    heightf_e, // height in feat 
    weight_e, 
    bmi_e, // the bmi element 
    bmi;     
    // Use getElementById to get a reference for the 
    // elements you will be using in your function 
    heighti_e=document.getElementById("heighti"); 
    heightf_e=document.getElementById("heightf"); 
    weight_e=document.getElementById("weight"); 
    bmi_e=document.getElementById("bmi"); 
    // Not all of these parenthesis are necessary but it 
    // helps to clear up the order of operation and avoid 
    // silly mistakes in long equations that are not 
    // broken up into several lines 
    bmi= (weight_e* 0.45)/(((12*heightf_e)+heighti_e) * 0.025)^2; 
    // set bmi to a string value 
    if(bmi<=19) 
    { 
    bmi="Underweight"; 
    } 
    else if(bmi>19 && bmi<=25) 
    { 
    bmi="Desirable"; 
    } 
    else if(bmi>25 && bmi<=29) 
    { 
    bmi="Prone to health risks"; 
    } 
    else if (bmi>29 && bmi<=40) 
    { 
    bmi="obese"; 
    } 
    else(bmi>40) 
    { 
    bmi="Extremely Obese"; 
    } 
    bmi_e.value=bmi; // bmi_a is the reference to the 
        // element in your form with the 
        // bmi id 
    return false; // make sure you return false to prevent 
        // page reload 
} 

我還沒有清理你的HTML表單,但至少現在它工作。我已將提交操作從按鈕移至表單標記。我還給出了身高體重和bmi輸入有意義的id名稱,所以我們可以在CalculateBMI函數中引用它們。

<form action="" name="bmi" onsubmit="return CalculateBMI()"> 
    <p> Weight:<p/></td> <td><input type="text" id="weight" name="weight" size="25" /> 

    <p>Height:</p> <input type="text" id="heightf" name="textbox" size="25" /><td><p>Ft.</p><input type="text" id="heighti" name="textbox" size="25" /> <p>In.</p> 

    <input type="submit" id="Calculate BMI" name="Calculate BMI" value="Calculate BMI" size="25" /> 
    According to the Panel on 
    Energy, Obesity, and Body Weigth Standards published by 
    American Journal of Clinical Nurttrition, your category is: 
    <input type="text" id="bmi" name="textbox" size="25" /> 
</form> 
+0

謝謝。這確實有很大的幫助。它可以幫助我更好地理解JavaScript,但不管結果如何,它都說我非常肥胖 – 11ash123