BMI计算

时间:2012-07-14 19:17:05

标签: javascript

计算体重指数的公式是重量* 703 /身高²。 创建一个包含三个文本框的网页:以磅为单位的重量,以英寸为单位的高度,以及包含BMI结果的文本框。使用名为calcBMI()的函数创建一个脚本,该函数使用权重和高度文本框中的值执行计算,并分配BMI文本框的结果。使用parseInt()函数将结果转换为整数。通过使用文档对象,表单名称以及每个文本框的名称和值属性(不使用函数参数),在函数内引用文本boxex。通过从按钮元素中的onclick事件调用函数来执行计算。

这是我能想到的:

<html><head>
<title>...</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

<script type="text/javascript">
/*<CDATA[[*/

function calcBMI(){
var weight, height, total;
document.form.height.value = weight * 703;
document.form.weight.value = (height * height);
var total = weight / height;
document.form.result.value = total;
}
/*]]>*/
</script>
</head>
<body>
<form name="form">
Weight: <input type="text" name="weight" /><br />
Height: <input type="text" name="height" /><br />
Result: <input type="text" name="result" /><br />
<input type="button" value="BMI Result!" onclick="calcBMI()" />
</form>

2 个答案:

答案 0 :(得分:1)

您正在引用表单的文档模型以显示答案,但不会读取您需要的值。您也没有像问题那样使用ParseInt。输入字段不需要onClick,只需要点击按钮。

祝好运作业:)

答案 1 :(得分:0)

通常,您遇到的问题是,在尝试获取文本框的值时,您尝试为文本框指定值。将您的代码更改为:

function calcBMI(){
  var weight, height, total;
  weight = document.form.weight.value; //take the value from the text box
  height = document.form.height.value; //take the value from the text box
  total = weight * 703 / height / height; //your formula
  document.form.result.value = parseInt(total); //assign the last text box the result
}
相关问题