1

私はjavascriptに精通していないので、ご容赦ください。

私はJavaScriptでコントロールを検証するフォームを持っています。フィールドがdivを介して空の場合、エラーが表示されますが、フォーカスしてテキストボックスに何かを入力すると、divが消えるはずです。しかし、エラーdivは表示されず、有効なものを入力してもdivが表示されます。

このスクリプトのどこが間違っているのか知りたいです:

<script type="text/javascript">
var err = document.getElementById("errmsg");

function checkInput(inPut) {

    if (inPut.getValue() == "") {
            err.setStyle('display', 'block');
            err.setTextValue("Field cannot be empty!");
            inPut.focus();
            return false;
    }
    else {
            return true;
    }

}


function checkTextBox(textBox)
{

    if (textBox.getValue() == "") {
          err.setStyle('display', 'block');
          err.setTextValue("Field cannot be empty!");
          textBox.focus();
         return false;
    }     
    else if (!checkValidity(textBox.getValue())) {
                 err.setStyle('display', 'block');
         err.setTextValue("Please enter a valid email address!");
         textBox.focus();
         return false;
    }
    else {
         return true;
    }
}

. . . 
<div id="errmsg" class="invalid" style="display:none;"></div> <br />

. . . 
<input type="text" tabindex="1" name="name" id="name" class="input_contact" onblur="checkInput(this);"/>  <br />

. . . 
<input type="text" tabindex="2" name="email" id="email" class="input_contact" onblur="checkTextBox(this);"/>  <br />

これは Facebook アプリのフォームですが、fbjs は機能しますが、基本的な JavaScript に問題があると思います。

4

2 に答える 2

3

これを試して

var err = document.getElementById("errmsg");

function checkInput(inPut) {

    if (inPut.getValue() == "") {
            err.setStyle('display', 'block');
            err.setTextValue("Field cannot be empty!");
            inPut.focus();
            return false;
    }
    else {
            err.setStyle('display', 'none');
            err.setTextValue("");
            return true;
    }

}


function checkTextBox(textBox)
{

    if (textBox.getValue() == "") {
          err.setStyle('display', 'block');
          err.setTextValue("Field cannot be empty!");
          textBox.focus();
         return false;
    }     
    else if (!checkValidity(textBox.getValue())) {
         err.setStyle('display', 'block');
         err.setTextValue("Please enter a valid email address!");
         textBox.focus();
         return false;
    }
    else {
         err.setStyle('display', 'none');
         err.setTextValue("");
         return true;
    }
}
于 2010-05-07T17:37:29.537 に答える
1

フィールドがチェックされたときではなく、最初に何かを入力したときにdivを非表示にするには、フィールドのonchangeおよび/またはonfocusイベントハンドラーも必要です。

<input type="text" tabindex="1" name="name" id="name" class="input_contact"
    onblur="checkInput(this);"
    onfocus="err.setStyle('display', 'none');"
    onchange="err.setStyle('display', 'none');"
/>

必要に応じて、内部checkInput()に設定することもできます。

于 2010-05-07T19:28:03.003 に答える