jQuery验证条件规则使输入永远无效-有效总是返回0 [英] jQuery Validation conditional rule makes input never valid - valid always returns 0

查看:117
本文介绍了jQuery验证条件规则使输入永远无效-有效总是返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对jQuery验证条件规则有疑问.

I'm having a problem with jQuery validation conditional rules.

我为这样的元素指定条件规则:

I specify a conditional rule for an element as such:

var validatorSettings = $("form").data('validator').settings; 
validatorSettings.rules = $.extend({}, validatorSettings.rules, {
        TimeFrameAmount: {
            number: function() { return $("select[name='TimeFrameUnits']").val() === "true"; },
            required: function() { return $("select[name='TimeFrameUnits']").val() === "true" } ;
        }
});

问题是我提交表单时,$("form").valid()返回0.

Problem is when I submit the form, $("form").valid() returns 0.

我追踪到下来,发现$("input[name='TimeFrameAmount']").valid()是表单中唯一返回0的元素,因此是导致表单无法通过验证的元素.

I traced is down to see that $("input[name='TimeFrameAmount']").valid() is the only element in the form that returns 0 and thus is the element causing the form to fail validation.

我进一步追溯到$("input[name='TimeFrameAmount']").valid() 总是会返回0,即使条件规则函数都返回false也会导致表单验证失败.

I further traced this down that $("input[name='TimeFrameAmount']").valid() always returns 0 and causes the form to fail validation even if the conditional rule functions both return false.

我通过在条件规则函数中设置断点来进行检查,然后从Firefox控制台触发了对$("input[name='TimeFrameAmount']").valid()的调用.

I checked this by setting a break point in the conditional rule functions, then triggered a call to $("input[name='TimeFrameAmount']").valid() from the Firefox console.

我的条件规则都返回false,并且$("input[name='TimeFrameAmount']").valid()仍然返回0,并且表单无法通过验证.

My conditional rules both return false and $("input[name='TimeFrameAmount']").valid() still returns 0 and the form fails validation.

要注意的一件事是,当我的条件规则返回false时,不会触发errorPlacement回调,这是有道理的,因为没有验证=没有错误.

One thing to note is that the errorPlacement callback in not fired when my conditional rules return false, which makes sense since no validation = no errors.

实际上,在我验证表单时,输入甚至具有有效"类(并且在以后仍然具有它)

In fact, the input even has the "valid" class at the time I validate the form (and still has it after)

但是,这些都不阻止valid()方法返回0

However, neither of those things prevent the valid() method from returning that 0

有什么想法如何使表格通过验证?

Any ideas how to get the form to pass validation?

推荐答案

列出添加条件规则的正确语法为:

Turns out the correct syntax to add conditional rules are:

TimeFrameAmount: { 
    required: { 
        depends : function () { 
            //check conditionality 
        } 
    } 
} 

这篇关于jQuery验证条件规则使输入永远无效-有效总是返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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