jQuery验证-隐藏错误消息并且不提交表单 [英] jQuery Validation - Hide error message and don't submit the form

查看:241
本文介绍了jQuery验证-隐藏错误消息并且不提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将jQuery Validate用于表单.

我有一个为required的字段,但是我不想显示该字段的任何错误消息.我只希望阻止表单提交.

我尝试将元素错误消息设置为此:

messages: {
    required: "" 
}

但是,由于我无权使用默认验证格式(这是我无法更改的通用文件),因此我仍然看到背景/框带有空白错误消息.

有没有办法隐藏特定元素的错误消息的呈现方式,同时仍然阻止表单提交?

解决方案

引用OP:

有没有办法隐藏特定元素的错误消息的呈现方式,同时仍然阻止表单提交?"

请参见 errorPlacement回调函数,该函数控制消息的放置位置.

使用条件语句,使您可以在特定元素上return false,这有效地防止了消息的放置,同时允许默认消息放置在其余元素上.

$('#myform').validate({
    rules: {
        ....
    },
    errorPlacement: function(error, element) {
        if (element.attr('name') == 'myName') {
            return false;  // no message placement
        } else {
            error.insertAfter(element); // default message placement
        }
    }
});

演示:http://jsfiddle.net/3cndvv3o/

I am using jQuery Validate for a form.

I have a field that is required, but I don't want to display any error message for the field. I just want the form submission to be blocked.

I have tried setting the element error message to this:

messages: {
    required: "" 
}

However, due to default validation format that I don't have access to (it is a common file that I cannot change), I still see a background/box with a blank error message.

Is there a way to hide the rendering of the error message for a specific element, while still preventing the form submission?

解决方案

Quote OP:

"Is there a way to hide the rendering of the error message for a specific element, while still preventing the form submission?"

See the errorPlacement callback function, which controls where messages are placed.

Use a conditional so that you return false on the specific element, which effectively prevents a message from being placed, while allowing the default message placement on the rest.

$('#myform').validate({
    rules: {
        ....
    },
    errorPlacement: function(error, element) {
        if (element.attr('name') == 'myName') {
            return false;  // no message placement
        } else {
            error.insertAfter(element); // default message placement
        }
    }
});

DEMO: http://jsfiddle.net/3cndvv3o/

这篇关于jQuery验证-隐藏错误消息并且不提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆