jquery 验证。使用选择器语法的自定义规则
我正在编写一个利用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此选择器:
如果第一个或第二部分为真,将触发
required
规则(请参阅多重选择器)。这就是为什么即使规则可见也会触发该规则的原因。如果您想让元素在 both 都隐藏时成为必需元素,您可能必须提供依赖函数而不是表达式:
至于
ignore
属性不起作用,那就是用于忽略与选择器匹配的字段(因此:hidden
将告诉验证器不要验证隐藏字段)。不确定你是否以这种方式使用它。This selector:
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:
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.