我目前正在创建一个可以用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 :(得分:1)
我看到一些可能导致您出现问题的事情。让我们从这句话开始:
var bmi= Math.write(weight*703)/(inch height)^2;
您没有定义重量或高度(您必须告诉它在文本框中查找或将其发送到它不会自动知道您指的是文本框的功能)。我期待像
这样的东西var weight = document.getElementById('weight').value;
高度和重量之间没有符号,这会引发语法错误,如果它们要在一起,你需要对它们做一些事情(并且意识到这不是仅仅以英寸为单位计算英尺)
var bmi =(重量* 703)/(英寸*高度)^ 2;
之后你正在使用if else - 这在你想说的Javascript中无效:
else if (19<bmi<=25)
最后,您没有返回值,也没有指定值应该去的WHERE。
var results;
if (bmi<=19)
{
results = "Underweight"
}
document.getElementById('results').value = results;
尝试实施其中一些建议,看看是否能让您走上正轨。
答案 1 :(得分:0)
您的javascript存在多个系统错误
例如1<a<12
无效,应写为a>1 && a<12
另外,你的CalculateBMI
函数不知道英尺或高度的重量高度(英尺)。您可以添加JQuery
以简化此操作,但document.getElementById
也可以。为了实现这一点,我还为表单变量提供了有意义的名称。
您没有在任何地方显示结果字符串。使用getElementById
,您还可以在html中找到结果元素,并将其值设置为计算结果。
如果您不希望在提交时刷新页面,则必须将表单设置为接收false
值onsubmit
。
bmi的数学语法已关闭,我假设它应该是(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>