-2

每个人。

我目前正在构建一个 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:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input id="fdegrees" type="text" /></p>
    <p style="text-align: left; margin-left: 280px">
        Degrees Celsius:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <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)" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input id="btnClear" type="button" value="Clear" onclick="btnClear_onclick(this.form)" /></p>

</body>
</html>

任何帮助将非常感激。

4

2 回答 2

2

else关键字不使用语句:

if(...) { } else { }

该行应该是else if

else if(checkNumber(theForm.cdegrees)) {...

于 2013-03-05T00:37:09.313 回答
1
  1. 你没有<form>所以不能用this.form。在您的输入周围添加<form>标签。
  2. 您的表单输入需要nameForm元素访问的属性
  3. 您应该将theForm作为参数添加到您的btnClear_onclick()函数并通过它访问输入
  4. Input.value返回一个字符串。字符串没有方法toFixed()。这是一种方法Number

这是一个工作示例 - http://jsfiddle.net/3x86T/1/

于 2013-03-05T00:46:59.487 回答