jQuery Validate 插件不验证不是表单输入的元素
我需要使用 jQuery 验证插件验证组合框组件:jqxComboBox
。该组件应用于 div
。因此定义
<div id="component_id" />
$( "#component_id" ).jqxComboBox({...});
$("#component_id" ).rules( "add", {
required: true,
messages:{
required: "Field is required"
}
});
会引发以下异常:无法读取未定义的属性“nodeType”
。我认为这应该是因为我在 div 上应用了验证规则。
该组件生成隐藏的输入类型以保存所选值。我还尝试在该隐藏组件上应用验证规则,但这不起作用:当隐藏没有值时也会提交表单。
$('input[type=hidden][name=myName]').rules( "add", {
required: true,
messages:{
required: "Field is required"
}
});
有人可以给我一些关于如何解决这个问题的提示吗?
I need to validate with jQuery Validation Plugin a combobox component: jqxComboBox
. This component is applied on a div
. So defining
<div id="component_id" />
$( "#component_id" ).jqxComboBox({...});
$("#component_id" ).rules( "add", {
required: true,
messages:{
required: "Field is required"
}
});
throws the following exception: Cannot read property 'nodeType' of undefined
. I think it should due to the fact that I'm applying validation rules on a div.
This component generate an input type hidden to hold selected value. I have also tryed to apply validation rules on that hidden component, but this do not works: form is submitted also when hidden has no value.
$('input[type=hidden][name=myName]').rules( "add", {
required: true,
messages:{
required: "Field is required"
}
});
Can someone please give me some hints on how to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
引用OP:
“我需要使用 jQuery Validation Plugin 验证组合框组件:jqxComboBox。此组件应用于 div。”
正如您所知,您不能使用 jQuery Validate验证
div
。您只能验证引用OP:
“该组件生成一个隐藏的输入类型来保存选定的值。我也尝试在该隐藏组件上应用验证规则,但这不起作用:表单也会在以下情况下提交: hidden 没有值。”
将值放入隐藏的输入元素中是一种可接受的解决方法。
但是,默认情况下,jQuery Validate 插件将忽略所有隐藏的输入元素。只需将
ignore
选项更改为[]
即可忽略“nothing”。在.validate()
调用中设置ignore
选项以及任何/所有其他选项。文档:http://jqueryvalidation.org/validate/#ignore
Quote OP:
"I need to validate with jQuery Validation Plugin a combobox component: jqxComboBox. This component is applied on a div."
As you learned, you cannot use jQuery Validate to validate a
div
. You can only validateinput
,select
andtextarea
elements that are within a<form>
container.Quote OP:
"This component generate an input type hidden to hold selected value. I have also tryed to apply validation rules on that hidden component, but this do not works: form is submitted also when hidden has no value."
Putting the value inside a hidden input element is an acceptable workaround.
However, by default, the jQuery Validate plugin will ignore all hidden input elements. Simply change the
ignore
option to[]
in order to ignore "nothing". Set theignore
option inside your.validate()
call along with any/all of your other options.Documentation: http://jqueryvalidation.org/validate/#ignore