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

发布于 2025-01-10 17:13:52 字数 678 浏览 3 评论 0原文

我需要使用 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 技术交流群。

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

发布评论

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

评论(1

泪是无色的血 2025-01-17 17:13:52

引用OP:

“我需要使用 jQuery Validation Plugin 验证组合框组件:jqxComboBox。此组件应用于 div。”

正如您所知,您不能使用 jQuery Validate验证 div。您只能验证

容器内的 inputselecttextarea 元素。

引用OP:

“该组件生成一个隐藏的输入类型来保存选定的值。我也尝试在该隐藏组件上应用验证规则,但这不起作用:表单也会在以下情况下提交: hidden 没有值。”

将值放入隐藏的输入元素中是一种可接受的解决方法。

但是,默认情况下,jQuery Validate 插件将忽略所有隐藏的输入元素。只需将 ignore 选项更改为 [] 即可忽略“nothing”。在 .validate() 调用中设置 ignore 选项以及任何/所有其他选项。

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

文档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 validate input, select and textarea 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 the ignore option inside your .validate() call along with any/all of your other options.

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

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

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