【问题标题】:JQuery Validate Plugin Error ClassJQuery 验证插件错误类
【发布时间】:2012-11-21 19:59:42
【问题描述】:

我想在验证函数之外编辑错误消息。所以我有一个简单的密码恢复表格。这是html:

<div style="position:relative; left:40%;" class="pass_recovery">
    <h2 class="title">Recover</h2>
    <form action="" method="POST" id="recover_pass">
        <span style="font-size:12px;">Email you registered with: </span><input type="text" name="recov_pass" id="recov_pass"<br>
        <input type="submit" value="Send Password">
    </form><br>
</div>

这是我用于验证的 jquery:

var pass_rec = $('#recover_pass').validate({
    onkeyup: false,
    errorClass: "password_messages",
    rules: {
        recov_pass: {
            required: true,
            email: true
        }
    },
    messages: {
        recov_pass: {
            required: "Please enter an email address",
            email: "Please enter a valid email address"
        }
    }
});

然后是 ajax 调用。这只是检查输入的电子邮件是否是有效的注册电子邮件地址。

$('#recover_pass').submit(function() {
    if(pass_rec.form()) {
        var $this = $(this);  
        $.ajax({
            data: $this.serialize(), // get the form data
            type: "POST", // GET or POST
            url: "Private/Recover_pass.php", // the file to call
            success: function(data) { // on success..
                if(data=="pass") { 
                    $('.password_messages').text('good');
                }
                else {
                    $('.password_messages').text('bad');
                }
            },
            error: function(data) {
                $('.password_messages').text('error');
            },
            complete: function(data) {
                $('#recov_pass').val("");
            }
        });
    }
    return false; //so it doesn't refresh when submitting the page
});

这不起作用,因为当我输入一个未注册的虚拟电子邮件时,比如example@ex.com,它不会显示bad 消息。如何在验证函数之外显示错误消息?

【问题讨论】:

  • Private/Recover_pass.php 返回什么?也许在你的成功函数中添加console.log(data)
  • @JasonSperske 我知道它会正确返回字符串。因为在此之前我只是一个&lt;p&gt;,我会在上面做.text(data),它会显示得很好。

标签: jquery html ajax validation jquery-validate


【解决方案1】:

您试图将结果字符串放入页面上具有“.password_messages”类的区域,但我在您的 html 示例代码中没有看到。你确定它在你页面的某个地方吗?

你应该有一个像这样的目标:

<span class='.password_messages'></span>

这就是您希望看到结果文本的地方,对吧?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多