简体   繁体   English

jQuery Validate插件未验证不是表单输入的元素

[英]jQuery Validate plugin not validating an element that's not a form input

I need to validate with jQuery Validation Plugin a combobox component: jqxComboBox . 我需要使用jQuery Validation Plugin来验证组合框组件: jqxComboBox This component is applied on a div . 此组件应用于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 . 引发以下异常: Cannot read property 'nodeType' of undefined I think it should due to the fact that I'm applying validation rules on a div. 我认为应该是由于我在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? 有人可以给我一些解决方法的提示吗?

Quote OP: 报价OP:

"I need to validate with jQuery Validation Plugin a combobox component: jqxComboBox. This component is applied on a div." “我需要使用jQuery Validation Plugin来验证组合框组件:jqxComboBox。此组件应用于div。”

As you learned, you cannot use jQuery Validate to validate a div . 如您所知,您不能使用jQuery Validate来验证div You can only validate input , select and textarea elements that are within a <form> container. 您只能验证<form>容器内的inputselecttextarea元素。

Quote OP: 报价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. 但是,默认情况下,jQuery Validate插件将忽略所有隐藏的输入元素。 Simply change the ignore option to [] in order to ignore "nothing". 只需将ignore选项更改为[]即可忽略“无”。 Set the ignore option inside your .validate() call along with any/all of your other options. 在您的.validate()调用中设置ignore选项,以及其他任何/所有其他选项。

$('#yourform').validate({
    // your other options, rules and callbacks,
    ignore: []  // <- ignore nothing - validate hidden elements
});

Documentation : http://jqueryvalidation.org/validate/#ignore 文档http : //jqueryvalidation.org/validate/#ignore

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM