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 回答