每个人。
我目前正在构建一个 Javascript HTML 页面。但是我无法让按钮工作,我不知道为什么。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></title>
<script language="javascript" type="text/javascript">
// <![CDATA[
function btnClear_onclick() {
fdegrees.focus();
fdegrees.value = "";
cdegrees.value = "";
}
function checkNumber(theTextBox) {
//check the text box for a value, if none alert user that there is a invalid entry
//the 'isNaN' checks to see if there is a number entered in the text box, and returns a boolean value
//as needed. (false is invalid, and true is valid)
//the 'isNaN' stands for 'is Not a Number'
if (theTextBox.value == "" || isNaN(theTextBox.value) || theTextBox.value.toFixed(1)) {
alert("Invalid Value in Text Box");
theTextBox.select();
return false;
}
//continue on with the calculations if valid using the btn Event.
else
return true;
}
function btnCompute_onclick(theForm) {
if (checkNumber(theForm.fdegrees)) {
num1 = parseInt(theForm.fdegrees.value);
answer = (num1 - 32) * 5 / 9;
theForm.cdegrees.value = answer;
}
else if (checkNumber(theForm.cdegrees)) {//error happens here on the square bracket
num2 = parseInt(theForm.cdegrees.value);
answer = num2 * 9 / 5 + 32;
theForm.fdegrees.value = answer;
}
}
// ]]>
</script>
</head>
<body>
<p style="text-align: center">
Fahrenheit/Celsius Converter</p>
<p style="text-align: left; margin-left: 280px">
Degrees Fahrenheit:
<input id="fdegrees" type="text" /></p>
<p style="text-align: left; margin-left: 280px">
Degrees Celsius:
<input id="cdegrees" type="text" /></p>
<p style="text-align: left; margin-left: 280px">
<input id="btnCompute" type="button" value="Compute" onclick="btnCompute_onclick(this.form)" />
<input id="btnClear" type="button" value="Clear" onclick="btnClear_onclick(this.form)" /></p>
</body>
</html>
任何帮助将非常感激。