[英]jQuery Validation error class affects Select element
When a validation error occurs on a <select>
form element after pressing the submit button, the errorClass
appears to be applied to the <select>
element which is not what I want. 在按下提交按钮后,在
<select>
表单元素上发生验证错误时, errorClass
似乎应用于<select>
元素,这不是我想要的。 errorClass
should only be applied to the error message. errorClass
应仅应用于错误消息。
Question: How can we avoid that problem? 问题:我们如何避免这个问题?
jsFiddle : http://jsfiddle.net/ufTn8/ jsFiddle : http : //jsfiddle.net/ufTn8/
jsfiddle : http://jsfiddle.net/SPK3D jsfiddle : http : //jsfiddle.net/SPK3D
If you take a look at updated code, when the error occurs, the class alert
is not applied to the error element until u click on the error element... this strange behavior is seen in both Chrome and Firefox. 如果您查看更新的代码,则在发生错误时,类
alert
不会应用于错误元素,直到您单击错误元素... Chrome和Firefox都会出现这种奇怪的行为。
Somehow $(element).siblings('label.error').addClass('alert');
不知何故
$(element).siblings('label.error').addClass('alert');
did not add the alert
class initially... 最初没有添加
alert
类...
You can use the highlight
and unhighlight
callback functions to apply whatever class you wish to whatever element you wish. 您可以使用
highlight
和unhighlight
回调函数将您希望的任何类应用于您希望的任何元素。 Below are the default functions. 以下是默认功能。 By including them as options inside
.validate()
, you can over-ride these defaults with whatever code you wish. 通过将它们作为选项包含在
.validate()
,您可以使用您希望的任何代码覆盖这些默认值。
highlight: function( element, errorClass, validClass ) {
if ( element.type === "radio" ) {
this.findByName(element.name).addClass(errorClass).removeClass(validClass);
} else {
$(element).addClass(errorClass).removeClass(validClass);
}
},
unhighlight: function( element, errorClass, validClass ) {
if ( element.type === "radio" ) {
this.findByName(element.name).removeClass(errorClass).addClass(validClass);
} else {
$(element).removeClass(errorClass).addClass(validClass);
}
}
EDIT : 编辑 :
Regarding OP's amended question, 关于OP的修正问题,
"Somehow
$(element).siblings('label.error').addClass('alert');
did not add the alert class initially..."“不知何故
$(element).siblings('label.error').addClass('alert');
最初没有添加警报类...”
That's because you are trying to select label.error
and it does not exist yet. 那是因为你试图选择
label.error
而它还不存在。 You must have a label.error
before you can select it and apply .alert
... those two things can't happen simultaneously. 你必须有一个
label.error
之前,你可以选择它并应用.alert
......这两件事情不能同时发生。
A workaround might include changing the default errorClass
for the plugin to .alert
and then using highlight
and unhighlight
to apply whatever other classes need to be applied to the select
element. 一种解决方法可能包括更改默认
errorClass
的插件.alert
,然后用highlight
和unhighlight
申请任何其他类需要被应用到select
的元素。
See: http://jsfiddle.net/SPK3D/1/ 请参阅: http : //jsfiddle.net/SPK3D/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.