jQuery 验证 - 隐藏错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4614685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery Validation - Hide Error Message
提问by Sjwdavies
I'm using the jQuery Validation plugin and want to disable the or element/container it creates to display the error 'message'.
我正在使用 jQuery 验证插件并希望禁用它创建的或元素/容器以显示错误“消息”。
Basically, I want the input element with the error to have the error class but NOT create an additional element containing the error message.
基本上,我希望带有错误的输入元素具有错误类,但不创建包含错误消息的附加元素。
Is this possible?
这可能吗?
I have just thought of a CSS workaround, but it doesn't really fix the fact that the element is still being created?
我刚刚想到了一个 CSS 解决方法,但它并没有真正解决仍在创建元素的事实?
<style>
label.simpleValidationError {display: none !important; }
</style>
回答by amichair
Use the Validator errorPlacement option with an empty function:
使用带有空函数的 Validator errorPlacement 选项:
$("selector").validate({ errorPlacement: function(error, element) {} });
This effectively places the error messages nowhere.
这有效地将错误消息置于无处。
回答by dabuda
You can also use this code:
您也可以使用此代码:
jQuery.validator.messages.required = "";
$("selector").validate();
回答by Frédéric Hamidi
You could set the showErrorsoption to a function that only performs element highlighting:
您可以将showErrors选项设置为仅执行元素突出显示的函数:
$("selector").validate({
showErrors: function() {
if (this.settings.highlight) {
for (var i = 0; this.errorList[i]; ++i) {
this.settings.highlight.call(this, this.errorList[i].element,
this.settings.errorClass, this.settings.validClass);
}
}
if (this.settings.unhighlight) {
for (var i = 0, elements = this.validElements(); elements[i]; ++i) {
this.settings.unhighlight.call(this, elements[i],
this.settings.errorClass, this.settings.validClass);
}
}
}
});
That relies heavily on the validation plugin's internals, though, so it's probably not safe. The best would be for the plugin to expose a defaultHighlightElements()
method the same way it does for defaultShowErrors()
, but it's not the case (yet).
但是,这在很大程度上依赖于验证插件的内部结构,因此它可能不安全。最好的办法是让插件以defaultHighlightElements()
与 for 相同的方式公开一个方法defaultShowErrors()
,但情况并非如此(目前)。
回答by luisqsm
I also had the same issue. I didn't want error messages, just the highlights of the form inputs that needed attention.
The messages were already empty like <?php $msj='""';?>
but still created the error elements/containers after the labels..
我也有同样的问题。我不想要错误消息,只想要需要注意的表单输入的亮点。消息已经是空的,<?php $msj='""';?>
但仍然在标签之后创建了错误元素/容器。
So to quickly stop this from happening I edited the jquery.validate.js file by commenting out the only line which states label.insertAfter(element);
around line 697 +/-..
因此,为了快速阻止这种情况发生,我编辑了 jquery.validate.js 文件,注释掉了第label.insertAfter(element);
697 行 +/- 周围的唯一一行。
It's just a noob's workaround ;) but it did it!
这只是菜鸟的解决方法 ;) 但它做到了!
回答by katalin_2003
I also wanted to hide the error messages and to turn my input
fields and textarea
red, on error.
Here's a small example :
我还想隐藏错误消息并在出错时将我的input
字段变为textarea
红色。这是一个小例子:
and fully working code in case jsfiddle breaks ;)
和完全工作的代码,以防 jsfiddle 中断;)
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title> Demo</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.10.1.js'></script>
<script type='text/javascript' src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.15.0/jquery.validate.js"></script>
<style type='text/css'>
.error {
border: 1px solid red;
}
</style>
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$( "#email_form" ).validate({
rules: {
email: {
required: true,
email: true
}
}, highlight: function(input) {
$(input).addClass('error');
},
errorPlacement: function(error, element){}
});
});//]]>
</script>
</head>
<body>
<form name="form" id="email_form" method="post" action="#">
<div class="item" style="clear:left;">
<label>E Mail *</label>
<input id="femail" name="email" type="text">
</div>
Type invalid email and press Tab key
</form>
</body>
</html>
回答by user1324471
As I am using CSS to style the label.valid, label.error in jquery validate, this button cleared all the errors and left and data in the form fields in tact.
由于我在 jquery 验证中使用 CSS 对 label.valid、label.error 进行样式设置,因此此按钮会机智地清除表单字段中的所有错误和左侧和数据。
<button onclick="$('label.error').css('display', 'none');return false;">Clear Error </button>
回答by nightcoder
Easiest solution with only CSS:
仅使用 CSS 的最简单解决方案:
label.valid {
display: none;
}
回答by Andy B
Possibly a far simpler and semantically preferable solution would be to use css
可能更简单且语义上更可取的解决方案是使用 css
label.error {
position:absolute;
left:20000px;
top:0;
}
input.error {
border:red 1px;
}
No JS required, easier to manage and someone with a screen reader will actually have some indication that they have missed something
不需要 JS,更易于管理,并且有屏幕阅读器的人实际上会有些迹象表明他们错过了一些东西
回答by Ramin
I had a project that developed by another validation library, I wanted to add validate library to use its features for check the form is valid or not (used validation library doesn't have that feature)
我有一个由另一个验证库开发的项目,我想添加验证库以使用其功能来检查表单是否有效(使用的验证库没有该功能)
My soloution was :I just added validate library by its CDN
我的解决方案是:我刚刚通过 CDN 添加了验证库
and use validate feature on click :
并在单击时使用验证功能:
$(document).on('click','#buttonID',function(){
var form = $( "#formID" );
form.validate();
console.log(form.valid());
});
and tried to hide error messages that raised by jquery validate by adding CSS code:
并尝试通过添加 CSS 代码来隐藏由 jquery validate 引发的错误消息:
label.error{
display: none!important;
}
回答by pensebien
You can add an onfocus and an onkeyup that would remove the error message.
您可以添加一个 onfocus 和一个 onkeyup 来删除错误消息。
$("selector").validate({
onkeyup: false,
onfocusout: false
});