此代码假设接受用户的 4 个主题分数并计算总数和平均值。它应该显示在文本框中。
当我单击计算(按钮)时,它应该生成一个警报对话框和一些计算,但事实并非如此。有什么错误?怎么了?我只是无法弄清楚这段代码有什么问题..对我来说一切都很好!
<!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>
<meta name="generator" content="HTML Tidy for Linux (vers 25 March 2009), see www.w3.org" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ass_3 q_6</title>
<script type="text/javascript" language="javascript">
function average()
{
alert("Hay");
var s1=document.getElementById("math").value;
var s2=document.getElementById("phy").value;
var s3=document.getElementById("che").value;
var s4=document.getElementById("bio").value;
var total=document.getElementById("tot");
var ave=document.getElementById("avg");
tot.value= (s1+s2+s3+s4);
avg.value=(tot.value)/4;
}
</script>
</head>
<body>
<h2 class="c1">Student Marks Average</h2>
<form name="myMarks" method="post" action="" id="myMarks">
<table align="middle" border="1">
<tr><td>Subjects</td><td>Marks Out of 100</td></tr>
<tr>
<td width="90px">Maths:</td>
<td><input type="text" id="math" name="maths" size="14" /></td>
</tr>
<tr>
<td>Physics:</td>
<td><input type="text" id="phy" name="physics" size="14" /></td>
</tr>
<tr>
<td>Chemistry:</td>
<td><input type="text" id="che" name="chemistry" size="14" /></td>
</tr>
<tr>
<td>Biology:</td>
<td><input type="text" id="bio" name="biology" size="14" /></td>
</tr>
<tr>
<td colspan="2" align="middle"><input type="button" value="Calculate" onclick="average()" /></td>
</tr>
<tr class="c2">
<td>Total:</td>
<td><input type="text" id="tot" name="total" size="14" /></td>
</tr>
<tr class="c3">
<td>Average:</td>
<td><input type="text" id="avg" name="average" size="14" /></td>
</tr>
</table>
</form>
</body>
</html>