jQuery验证组中的要求不起作用 [英] Jquery validate require from group not working

查看:78
本文介绍了jQuery验证组中的要求不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Having some trouble with this function of the validation plugin: http://jqueryvalidation.org/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, ".org-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>

请注意,所有checkboxesname均为"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, ".org-group"]
    }
  }
});

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

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

这篇关于jQuery验证组中的要求不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆