0

エラーメッセージを個別に表示するのではなく、入力要素自体にエラーメッセージを表示したいと思います。メッセージを表示する必要がある要素をどのようにキャプチャしますか?

このコード:

function checkForm(form) { 

        if (yname.getValue() == "" || yname.getValue() == "Your Name" || name.getValue() == "" || name.getValue() == "Name of the Book" || url.getValue() == "" || url.getValue() == "URL of the Book"){
              [id-of-the-element].setTextValue("Field cannot be empty!");
             return false;
        }  
4

2 に答える 2

1

条件ステートメントを「拡張」できます。

function checkForm(form) {
    var result = true;
    if (yname.getValue() == "" || yname.getValue() == "Your Name") {
        setErrorMessage(yname);
        result = false;
    } else if (name.getValue() == "" || name.getValue() == "Name of the Book") {
        setErrorMessage(yname);
        result = false;
    } else if (url.getValue() == "" || url.getValue() == "URL of the Book") {
        setErrorMessage(yname);
        result = false;
    }
    return result;
}

function setErrorMessage(field) {
    field.setTextValue("Field cannot be empty!");
}
于 2010-07-22T15:09:09.177 に答える
0

jqueryを使用していない場合、これはプレーンなJavaスクリプトコードです。

document.getElementById( "id-of-the-element")。value = "フィールドを空にすることはできません!";

于 2010-07-22T14:59:22.767 に答える