请帮我处理以下代码,因为我不知道我错过了什么。单击按钮计算应触发功能computeAll()
。相反,脚本不会对我的行为做出反应:
html代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
<script src="complex.js"></script>
</head>
<body>
<form name="calcul" method ="post">
<table>
<tr><th></th><th>Real part</th><th>Imaginary part(i)</th></tr>
<tr><td>Enter the first complex number</td><td><input type="text" name="real1"/></td>
<td><input type="text" name="imaginary1"/></td></tr>
<tr><td>Enter the secound complex number</td><td><input type="text" name="real2"/></td>
<td><input type="text" name="imaginary2"/></td></tr>
<tr><td>Enter your choice</td><td colspan="2"><div><p>Addition<input type="radio" value="add" checked name="option">
</p><p>Substraction<input type="radio" value="subs" name="option"></p></div>
</td></tr>
<tr><td >The answer is:</td><td><input type="text" name="displayReal"/></td><td><input type="text" name="displayImaginary"/></td></tr>
<tr><td colspan="3"></td></tr>
<tr>
<td><input type="button" value="Compute" onclick="computeAll()"></td>
<td><input type="button" value="Clear" onclick="clearAll()"></td>
</tr>
</table>
</form>
</body>
</html>
和java脚本代码
<script type="text/javascript">
function computeAll() {
var val1, val2, img1, img2, resultR, resultI
val1 = parseInt(document.calcul.real1.value);
val2 = parseInt(document.calcul.real2.value);
img1 = parseInt(document.calcul.imaginary1.value);
img2 = parseInt(document.calcul.imaginary2.value);
if (calcul.option[0].checked){
resultR = val1 + val2;
resultI = img1 + img2;
document.calcul.displayReal.value = resultR;
document.calcul.displayImaginary.value = resultI;
}
if (calcul.option[1].checked){
resultR = val1 - val2;
resultI = img1 - img2;
document.calcul.displayReal.value = resultR;
document.calcul.displayImaginary.value = resultI;
}
}
</script>
答案 0 :(得分:2)
<script>
标记位于何处?在<head>
中引用的complex.js文件中?您不应该在JavaScript文件中需要<script>
标记,这可能会导致问题。检查浏览器的开发人员工具是否存在任何JS错误。
答案 1 :(得分:1)
当您引用为外部文件时(例如<script type="text/javascript">
),您无需将javascript代码放在</script>
和<script src="complex.js"></script>
中。删除它,然后重试。