JQuery中的条件规则验证 [英] Conditional Rule Validation in JQuery

查看:90
本文介绍了JQuery中的条件规则验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 JQuery验证.现在,我希望仅在满足某些条件时才调用规则,即,我希望$("#AppSelectField").is(':hidden')返回false,然后才调用规则.

I am using JQuery Validation. Now I want my rules to be invoked only when certain condition is satisfied, namely, I want $("#AppSelectField").is(':hidden') to return false, only then I invoke the rules.

我的规则如下:

$(function() {
    $("#RequestLock").validate({
        rules: {
        FloorNum:{
        required: true,
        digits: true
        },


    },
    message: {
     FloorNum: "The floor number must be integer",

},

});
});

如何修改以上部分以满足我的需求? 请注意,我不想为requireddigits编写自己的自定义方法.

How to modify the above section to satisfy my needs? Note I don't want to write my own custom method for required and digits.

推荐答案

这应该做到:

$(function() {
    var checkIfFieldVisible = function() {
        return !$("#AppSelectField").is(':hidden');
    };

    $("#RequestLock").validate({
        rules: {
            FloorNum: {
                required: { depends: checkIfFieldVisible },
                digits: { depends: checkIfFieldVisible }
            }, // <-- EXTRA COMMA
        },
        message: {
            FloorNum: "The floor number must be integer",
        }, // <-- EXTRA COMMA
    });
});

(我在代码中标记了一些多余的逗号,因为它们会使IE阻塞)

(I marked some extra commas you have on your code, as they will make IE choke)

这篇关于JQuery中的条件规则验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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