jQuery验证组中的要求不起作用

编程入门 行业动态 更新时间:2024-10-23 22:33:47
本文介绍了jQuery验证组中的要求不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

验证插件的此功能有些麻烦: jqueryvalidation/require_from_group-method

Having some trouble with this function of the validation plugin: jqueryvalidation/require_from_group-method

表格的相关部分:

<input class="org-group" type="checkbox" name="org[1]" id="org[1]" value="on"> <input class="org-group" type="checkbox" name="org[2]" id="org[2]" value="on"> <input class="org-group" type="checkbox" name="org[3]" id="org[3]" value="on">

此复选框列表是动态的.它可能有2个条目或50个条目,这就是为什么我在表单中使用数组.我希望该插件确保已选中org-group类中的xxx复选框之一.

This list of checkboxes is dynamic. It could have 2 entries, or 50 entries, that's why I am using an array in the form. I want the plugin to ensure that one of the xxx checkboxes in the org-group class is checked.

我在插件中使用了它

$( "#orgform" ).validate({ rules: { org[]: { require_from_group: [1, "-group"] },

但是,它不起作用.无论选中多少盒子,表单都将提交. 提交处理程序"工作正常.我还需要这种形式的其他一些文本字段.如果未填写这些字段,则验证运行良好.

However, it's not working. The form just submits regardless the amount of boxes checked. The "submithandler" works fine. I have some other text fields in this form which are required. If these fields are not filled, the validation runs fine.

我在做什么错了?

推荐答案

您需要确保已引用了所有必需的JQuery库.如果您仅使用核心库,则验证将无法进行.

You need to make sure you've referenced all of the required JQuery libraries. If you're using just the core library, validation won't work.

确保您还引用了其他方法.min.js

小提琴示例

拥有所有必需的引用后,您将需要修改html内容,以便所有复选框均具有相同的name.如果所有名称都是唯一的,验证将不起作用. ids显然必须保持唯一性.

Once you have all of the required references, you'll need to modify your html content, so that all of your check boxes have the same name. Validation won't work if all of the names are unique. The ids will obviously have to remain unique.

更新的HTML :

<form id="orgform"> <input class="org-group" type="checkbox" name="org" id="org[1]" value="on"> <input class="org-group" type="checkbox" name="org" id="org[2]" value="on"> <input class="org-group" type="checkbox" name="org" id="org[3]" value="on"> <input class="org-group" type="checkbox" name="org" id="org[4]" value="on"> <input type="submit" value="Validate"> </form>

请注意,所有checkboxes的name均为"org".

Notice that all of the checkboxes have a name of "org".

更新html后,将对validate的调用更新为引用"org"而不是"org []".如果您检查控制台,则会注意到以下错误:

After you've updated your html, update the call to validate to reference "org" instead of "org[]". If you inspect the console you'll notice the following error:

Uncaught SyntaxError: Unexpected token [

这是因为rules属性不接受[或]

This is because the rules property doesn't accept [ or ]

随着html的更改,您还需要更新JQuery.

With the html changes, you'll also need to update your JQuery.

更新的jQuery:

$("#orgform").validate({ rules: { org: { require_from_group: [1, "-group"] } } });

注意:请参阅我上面链接的Fiddle示例以获取工作示例.

Note: Please see the Fiddle example I've linked above for a working example.

更多推荐

jQuery验证组中的要求不起作用

本文发布于:2023-11-01 23:15:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550692.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组中   不起作用   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!