BMI calculations

BMI calculations

本文关键字:calculations BMI      更新时间:2023-10-02

体重指数的计算公式为weight*703/height²。创建一个包含三个文本框的网页:体重(磅)、身高(英寸),以及一个包含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>

您引用表单的文档模型是为了显示答案,而不是为了读取所需的值。你也没有像问你的那样使用ParseInt。输入字段不需要像那样的onClick,只需要你要点击的按钮。

祝你的家庭作业好运:)

通常,您面临的问题是,当您应该尝试获取文本框的值时,却试图为文本框赋值。将您的代码更改为:

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
}