HTML JavaScript按钮不起作用,以及令人困惑的错误

时间:2013-03-05 00:32:45

标签: javascript html

大家。

我目前正在构建一个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>

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

else关键字不使用语句:

if(...) { } else { }

该行应为else if

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

答案 1 :(得分:1)

  1. 您没有<form>,因此无法使用this.form。在您的输入周围添加<form>标记。
  2. 您的表单输入需要name元素}属性访问Form属性
  3. 您应该将theForm作为参数添加到btnClear_onclick()函数中并通过该
  4. 访问输入
  5. Input.value返回一个字符串。字符串没有方法toFixed()。这是Number
  6. 的方法

    这是一个有效的例子 - http://jsfiddle.net/3x86T/1/