它应该做的是将文本框中的每个数字相加,然后通过将其除以 5 来计算平均值。我必须使用 onchange 事件处理程序和 2 个函数并将结果返回给 calcAvg 函数。对于每个文本box 添加一个 onchange 事件处理程序,该处理程序调用名为 calcavg() 的函数并通过引用其文档对象将文本框的值传递给该函数。在 performCalc() 函数中计算五个数字的平均值,然后将结果返回给 calcAvg 函数这是我所拥有的:
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function calcAvg(one, two, three, four, five){
var one = document.totalf.one.value;
var two = document.totalf.two.value;
var three = document.totalf.three.value;
var four = document.totalf.four.value;
var five = document.totalf.five.value;
return}
function performCalc() {
var one = document.totalf.one.value;
var two = document.totalf.two.value;
var three = document.totalf.three.value;
var four = document.totalf.four.value;
var five = document.totalf.five.value;
var res = parseFloat(one + two + three + four + five);
var calcResult = parseFloat(res/5);
return}
</script>
</head>
<body>
<form name="totalf" action="%20">
<p>Input <input type="text" value="0" name="one" onchange="calcAvg(document.totalf.one.value)"></p>
<p>Input <input type="text" value="0" name="two" onchange="calcAvg(document.totalf.two.value)"></p>
<p>Input <input type="text" value="0" name="three" onchange="calcAvg(document.totalf.three.value)" ></p>
<p>Input <input type="text" value="0" name="four" onchange="calcAvg(document.totalf.four.value)"></p>
<p>Input <input type="text" value="0" name="five" onchange="calcAvg(document.totalf.five.value)"></p>
<p>Result:<input type="text" name="res"></p>
</form>
</body>
</html>