-1

体重指数的计算公式是体重*703/身高²。创建一个包含三个文本框的网页:体重(磅)、身高(英寸)和一个包含 BMI 结果的文本框。使用名为 calcBMI() 的函数创建一个脚本,该函数使用体重和身高文本框中的值执行计算,并分配 BMI 文本框的结果。使用 parseInt() 函数将结果转换为整数。使用每个文本框的文档对象、表单名称以及名称和值属性从函数内引用文本框(不要使用函数参数)。通过从按钮元素中的 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>
4

2 回答 2

1

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

祝作业好运:)

于 2012-07-14T19:21:17.090 回答
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
}
于 2012-07-14T22:06:06.283 回答