jQuery验证:验证用于至少一个复选框从列表,这里的名字是不同的选择 [英] jQuery Validation: validate atleast one checkbox is selected from a list where names are different

查看:1556
本文介绍了jQuery验证:验证用于至少一个复选框从列表,这里的名字是不同的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要验证用于至少一个复选框使用jQuery的验证插件复选框的列表中选择。由于该复选框是一个ASP.NET GridView控件的一部分,这些复选框的名称会有所不同,这使得对验证插件复杂,因为它预计该规则的名称设置规则。我寻觅了一圈,发现以下问题

I need to validate that atleast one check box is selected from a list of checkboxes using jQuery Validation Plugin. Since the checkboxes are part of a ASP.NET GridView Control, the names of these checkboxes will be different, which makes setting up the rules for the validation plugin complex since it expects the name for the rule. I have searched around and found the below questions

  • Question 1710486
  • Question 1136507

我想到的第一个问题是一个更好的解决方案,但我仍然觉得这是不该做的正确方法。没有任何人对这个问题的任何更多的选择。

I think the first question is a much better solution, but i still feel that it is not the right way to do. does anybody else have any more options for this problem.

推荐答案

怎么样一个复杂的规则,才有,如果没有其他复选框被选中所需的项目。

How about a complex rule that only makes the item required if no other checkboxes have been checked.

rules: {
   'require-one': {
       required : {
           depends: function(element) {
                       var allBoxes = $('.require-one');
                       if (allBoxes.filter(':checked').length == 0) {
                          if (allBoxes.eq(element).length != 0) {
                              return true;
                          }
                       }
                       return false;
                    }
       }
    }
}

然后,你的需要一类应用到集合中的每个复选框。如果没有的箱子,其中检查第一个将被要求

Then you'd apply the require-one class to each checkbox in the set. The first one would be required if none of the boxes where checked.

这篇关于jQuery验证:验证用于至少一个复选框从列表,这里的名字是不同的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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