-2

みんな。

現在、JavascriptHTMLページを作成しています。ただし、ボタンを機能させることができず、理由がわかりません。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. フォーム入力には、要素nameがアクセスできる属性が必要ですForm
  3. theForm関数に引数として追加し、btnClear_onclick()それを介して入力にアクセスする必要があります
  4. Input.value文字列を返します。文字列にはメソッドがありませんtoFixed()。これはNumber

これは実際の例です - http://jsfiddle.net/3x86T/1/

于 2013-03-05T00:46:59.487 に答える