我需要创建一个接受用户输入(整数)的表单,将该输入添加到数组中,然后对整数求和,并将它们的总数作为文本显示在网页上。我觉得我已经接近解决方案了,但是当我按下提交按钮时,什么也没有发生。我对 Javascript 很陌生,这是一个课堂作业。谁能判断这段代码是否正确或建议哪里可能有错误?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Adding Numbers</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<p><script type="javascript">
/* <![CDATA[ */
function addNums() {
var total = 0;
var intergers = new Array(5);
intergers[0] = document.forms[0].element[0].value;
intergers[1] = document.forms[0].element[1].value;
intergers[2] = document.forms[0].element[2].value;
intergers[3] = document.forms[0].element[3].value;
intergers[4] = document.forms[0].element[4].value;
for (i=0; i<intergers.length; i++) {
total += intergers[i];
}
return total;
document.write("The total for the intergers entered is " + total + "."</p>);
}
/* ]]> */
</script><strong>Please enter an interger into each of the following text boxes.<br />
Press the Submit button to see a total of all the intergers.</strong></p>
<form action="" name="intergers" onsubmit="addNums();" >
<p>1st Interger<br/>
<input type="text" name="num1" value="" size="10" /></p>
<p>2nd Interger<br/>
<input type="text" name="num2" value="" size="10" /></p>
<p>3rd Interger<br/>
<input type="text" name="num3" value="" size="10" /></p>
<p>4th Interger<br/>
<input type="text" name="num4" value="" size="10" /></p>
<p>5th Interger<br/>
<input type="text" name="num5" value="" size="10" /></p>
<p><input type="button" name="submit" value="Submit" /></p>
</form>
</body>
</html>`enter code here`