2016-12-06 119 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