jquery 验证。使用选择器语法的自定义规则

发布于 2024-12-11 20:32:30 字数 1568 浏览 0 评论 0原文

我正在编写一个利用 jQuery 验证的系统,该系统通过 json 引入自定义规则、消息并动态验证单个页面中的多个表单。一切都很顺利,除了我找不到任何关于自定义规则的正确语法的像样信息......

例如:

我知道这有效......

"ui_txt_GCPostcode": {
    "required": "input[name=ui_rb_ItemAddress][value=Recipient]:checked"
}

我在这里说 ui_txt_GCPostcode 是必需的当选中列表中名称为 ui_rb_ItemAddress 且值为 Recipient 的单选按钮时。

在我看来,我可以根据 依赖表达式 包含特定的选择器属性。

然而,这不起作用......

"ui_sel_PCSelect": {
        "required": "input[name=ui_txt_PostCodeSearch]:hidden, input[name=ui_txt_Address]:hidden"
    }

即使我有 ui_txt_Address 可见,验证器仍在触发。

我什至使用 ignore 隐藏属性设置验证器,例如。

                // Validate the form once the defaults are set.
                $validator = $form.validate({
                    // This prevents validation from running on every
                    // form submission by default.
                    onsubmit: false,
                    messages: messages,
                    rules: rules,
                    errorContainer: $container,
                    errorLabelContainer: $("ol", $container),
                    wrapper: "li",

                    // Ignore hidden items. Why is this not working??
                    ignore: ":hidden"
                });

有什么想法吗?我的截止日期非常紧迫,我开始恐慌。

I'm writing a system utilizing jQuery Validation that pulls in custom rules, messages via jSON and dynamically validates multiple forms in single pages. All is going swimmingly except I cannot find any decent information on the correct syntax for custom rules....

For example:

I know this works....

"ui_txt_GCPostcode": {
    "required": "input[name=ui_rb_ItemAddress][value=Recipient]:checked"
}

I'm saying here that ui_txt_GCPostcode is required only when a radio button from a list with the name ui_rb_ItemAddress and the value Recipient is checked.

This to me looks like I am able to assign ruled based upon dependency expressions containing specific selector attributes.

This however doesn't work.....

"ui_sel_PCSelect": {
        "required": "input[name=ui_txt_PostCodeSearch]:hidden, input[name=ui_txt_Address]:hidden"
    }

The validator is firing even though I have ui_txt_Address visible.

I'm even setting up the validator with the ignore hidden property eg.

                // Validate the form once the defaults are set.
                $validator = $form.validate({
                    // This prevents validation from running on every
                    // form submission by default.
                    onsubmit: false,
                    messages: messages,
                    rules: rules,
                    errorContainer: $container,
                    errorLabelContainer: $("ol", $container),
                    wrapper: "li",

                    // Ignore hidden items. Why is this not working??
                    ignore: ":hidden"
                });

Any ideas?? I'm on a pretty tight deadline and I'm starting to panic.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放肆 2024-12-18 20:32:30

此选择器:

"input[name=ui_txt_PostCodeSearch]:hidden, input[name=ui_txt_Address]:hidden"

如果第一个第二部分为真,将触发required规则(请参阅多重选择器)。这就是为什么即使规则可见也会触发该规则的原因。

如果您想让元素在 both 都隐藏时成为必需元素,您可能必须提供依赖函数而不是表达式:

"ui_txt_GCPostcode": {
    "required": function() {
        return $("input[name='ui_rb_ItemAddress'][value='Recipient']:checked").length &&
            $("input[name='ui_rb_ItemAddress'][value='Recipient']:checked").length;
    }
}

至于 ignore 属性不起作用,那就是用于忽略与选择器匹配的字段(因此 :hidden 将告诉验证器不要验证隐藏字段)。不确定你是否以这种方式使用它。

This selector:

"input[name=ui_txt_PostCodeSearch]:hidden, input[name=ui_txt_Address]:hidden"

Will trigger the required rule if either the first or second part are true (see the multiple selector). This is why the rule fires even if one is visible.

If you want to make the element required if both are hidden, you may have to supply a dependency-function instead of an expression:

"ui_txt_GCPostcode": {
    "required": function() {
        return $("input[name='ui_rb_ItemAddress'][value='Recipient']:checked").length &&
            $("input[name='ui_rb_ItemAddress'][value='Recipient']:checked").length;
    }
}

As for the ignore property not working, that's for ignoring fields matching the selector (so :hidden will tell validator not to validate hidden fields). Not sure if you were using it this way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文