jQuery验证和组 - 当第二个字段值正确时无法正常工作? [英] jQuery validate and groups - doesn't work properly when second field value is correct?

查看:59
本文介绍了jQuery验证和组 - 当第二个字段值正确时无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些字段,它们是组的一部分 - 演示
当用户在我的演示中的第一个字段(数字(1到10))中输入错误数据时,会显示错误消息。但是,当用户在第二个字段(数字(11到20))中输入正确的数据时,如果组中的一个字段仍包含不正确的数据,则此消息将消失。此外,错误类被删除(这在演示中不可见)。

I have some fields, which are part of group - demo. When user inputs wrong data in first field (Digits (1 to 10) in my demo), then error message is shown. But when user inputs correct data in the second field (Digits (11 to 20)), this message disappears, when one field in the group still contains incorrect data. Moreover, error class is removed (this is not visible in the demo).

如何更正?

推荐答案

这是jQuery验证组的一个已知事实。

It's a known fact this problem with groups on jQuery validation.

我的解决方案非常简单。

My solution is very simple.

首先,你需要为所有人添加相同的类您在群组中的输入:

First of all, you need to add the same class to all of your inputs in the group:

<input id="firstInGroup" class="groupBug">
<input id="secondInGroup" class="groupBug">
<input id="thirdInGroup" class="groupBug">

然后,您为该类创建一个触发器

Then, you create a trigger to that class

    $('.groupBug').change(function(){
    if($(this).siblings().valid()){
        //all items of the group are valid, do nothing or manually add valid class
    }else{
        //add the error class to the parent div, or whatever you want for the error
    }    

它为我提供了几个选择。

It worked for me with a couple of selects.

这篇关于jQuery验证和组 - 当第二个字段值正确时无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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