简体   繁体   English

jQuery Validation错误类影响Select元素

[英]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/ jsFiddlehttp//jsfiddle.net/ufTn8/

在此输入图像描述


Update 更新

jsfiddle : http://jsfiddle.net/SPK3D jsfiddlehttp//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. 您可以使用highlightunhighlight回调函数将您希望的任何类应用于您希望的任何元素。 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 ,然后用highlightunhighlight申请任何其他类需要被应用到select的元素。

See: http://jsfiddle.net/SPK3D/1/ 请参阅: http//jsfiddle.net/SPK3D/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM