为什么jquery.validate.js会显示两次错误消息?

时间:2017-12-25 06:30:21

标签: javascript jquery jquery-validate

我正在尝试使用jQuery验证创建表单,这是我的表单, 然后我使用此代码验证输入。

这很简单:

$('#forgot_pwd').validate({
    debug: true,
    rules: {
        username: {
            minlength: 6,
            required: true,

        },
        email: {
            required: true,
            email: true
        }
    },
    messages: {
            username: {
                required: "Enter your Username"
            },
            email: {
                required: "Enter your Email",
                email: "Enter valid email address"
            },
        },

     errorPlacement: function(error, element) {
            error.hide();
            $('.messagebox').hide();
            error.appendTo($('#alert-message'));
            $('.messagebox').slideDown('slow');



        },
    highlight: function(element, errorClass, validClass) {
            $(element).parents('.control-group').addClass('error');
        },
        unhighlight: function(element, errorClass, validClass) {
            $(element).parents('.control-group').removeClass('error');
            $(element).parents('.control-group').addClass('success');
        }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.min.js"></script>

<div class="card">
    	<h4 class="l-login">Forgot Password</h4>
    	<div class="messagebox">
    		<div id="alert-message"></div>
    	</div>
    	<form class="col-md-12" id="forgot_pwd" method="POST">
    			<div class="form-group form-float">
    				<div class="form-line">
    					<input type="text" class="form-control" id="username" name="username">
    					<label class="form-label">Username</label>
    				</div>
    			</div>
    			<div class="form-group form-float">
    				<div class="form-line">
    					<input type="email" class="form-control" id="email" name="email">
    					<label class="form-label">Email</label>
    				</div>
    			</div>
    			
    			<button id="signin" type="submit" class="btn btn-raised waves-effect bg-red" data-loading-text="Loading...">Reset Password</button>
    
    			<a href="sign-up.php" class="btn btn-raised waves-effect" type="submit">SIGN UP</a><br/><br/>
    		</form>
    </div>

至于我,我只调用错误消息框一次,但它显示两次。 不知道为什么会出现两次。

为什么会这样?

2 个答案:

答案 0 :(得分:1)

尝试使用invalidHandler功能将其清空。

invalidHandler: function() {
    $('#alert-message').empty();
}

当提交表单并且规则失败时,这将清空#alert-message。这样可以多次调用errorPlacement函数,而不会在多个错误上清空#alert-message

例如在您的代码中。

$('#forgot_pwd').validate({
    debug: true,
    rules: {
        username: {
            minlength: 6,
            required: true,

        },
        email: {
            required: true,
            email: true
        }
    },
    messages: {
        username: {
            required: "Enter your Username"
        },
        email: {
            required: "Enter your Email",
            email: "Enter valid email address"
        },
    },

    errorPlacement: function(error, element) {
        error.hide();
        $('.messagebox').hide();
        error.appendTo($('#alert-message'));
        $('.messagebox').slideDown('slow');
    },
    highlight: function(element, errorClass, validClass) {
        $(element).parents('.control-group').addClass('error');
    },
    unhighlight: function(element, errorClass, validClass) {
        $(element).parents('.control-group').removeClass('error');
        $(element).parents('.control-group').addClass('success');
    },
    invalidHandler: function() {
        $('#alert-message').empty();
    }
});

答案 1 :(得分:0)

我认为您应该在向其添加新消息之前清除$(&#39;#alert-message&#39;)。

{{1}}