0

我的表单中有这个 jQuery 验证控件。它正在工作:

$("#webform-client-form-45").submit(function(){

  if($("#edit-submitted-name-surname").val() == "" || $("#edit-submitted-name-surname").val() == "Name Surname"){

        return false;
    }else if($("#edit-submitted-telephone").val() == "" || $("#edit-submitted-telephone").val() == "Telephone"){

        return false;
    }else if($("#edit-submitted-e-posta").val() == "" || $("#edit-submitted-e-posta").val() == "E-MAIL"){

        return false;

    }else{  if($("#edit-submitted-messages").text() == "" || $("#edit-submitted-messages").text() == "Message"){

                return false;
            }else
        return true;
    }
});

但是当用户将该字段留空或在此字段中写入错误时,我想显示一个警报。例如,“请输入电子邮件地址””

我怎样才能做到这一点?

4

1 回答 1

1

使用警告框:

if($("#edit-submitted-name-surname").val() == "" || $("#edit-submitted-name-surname").val() == "Name Surname")
{
alert("Please input your name");
return false;
}

使用 div:

//html

<input type = "text" name = "edit-submitted-name-surname" id = "edit-submitted-name-surname" />
<div class = "error" id = "name_error">Please input your name</div>

//css

.error{ display: none; color: red;}

//jQuery

Put this at the top of the function: $('.error').hide();

if($("#edit-submitted-name-surname").val() == "" || $("#edit-submitted-name-surname").val() == "Name Surname")
    {
    $('#name_error').show();
    return false;
    }
于 2012-05-15T11:18:34.543 回答