简体   繁体   English

验证至少一个复选框(多组复选框)

[英]validation at least one checkbox (multiple group of checkboxes)

I know this've been answered in here 我知道这里已经回答
But my html looks like below : 但我的html如下所示:

<div >
    <label> <input type="checkbox" name="service_area[]" value="colorado">colorado</label>
    <label> <input type="checkbox" name="service_area[]" value="michigan">michigan</label>
</div>

<div >
    <label> <input type="checkbox" name="fav[]" value="skiing">skiing</label>
    <label> <input type="checkbox" name="fav[]" value="volley">volley</label>
</div>

Objectives : 目标:

  • name must have array, in here : service_area[] 名称必须具有数组,在这里:service_area []
  • must show only one error message 必须仅显示一条错误消息

How to achieve this ? 如何实现呢?
Each time I use jvalidate with '[]' name, it seems that it only reflects to the first element, not all elements. 每次我将jvalidate与'[]'名称一起使用时,似乎只反映到第一个元素,而不是所有元素。

UPDATE: 更新:

I forget to mention that I have more than one group of checkboxes that needed to be checked. 我忘了提到我有不止一组复选框需要检查。
Updated above html. 在html上方更新。
So, rynhe answer inspired me a bit : 因此,林恩的答案给了我一些启发:

$.validator.addMethod(
  "atleastone",
  function(value, element, params) {
    return $('[name="' + element.name +'"]:checked').size() > 0;
  },
  'at least one');

  rules: {
    'service_area[]': {
      atleastone : true,
    },
    'fav[]': {
      atleastone : true,
    },        

The most important part is 'service_area[]' . 最重要的部分是'service_area[]' Above code works fine for me. 上面的代码为我工作得很好。 Thx ~ 谢谢〜

As per Tats_innit answer which is in validation for at least one checkbox 根据Tats_innit答案,该答案至少针对一个复选框

you can change this line 您可以更改此行

var checkboxes = $('.require-one');

to

var checkboxes = $('input[name="river[]"]');

as per your html element var checkboxes = $('input[name="service_area[]"'); 根据您的html元素var checkboxes = $('input[name="service_area[]"');

Live Demo 现场演示

You do not need to add any special methods in this case. 在这种情况下,您无需添加任何特殊方法。 When every checkbox in one group shares the same name , simply use the required rule . 当一个组中的每个复选框都具有相同的name ,只需使用required规则 Since the names contain brackets, [] , enclose it with quotes. 由于名称包含方括号[] ,因此请用引号引起来。

$(document).ready(function () {

    $('#myform').validate({
        rules: {
            'service_area[]': {
                required: true
            },
            'fav[]': {
                required: true
            }
        }
    });

});
  • At least one checkbox from service_area[] group is required. service_area[]组中至少需要一个复选框。

  • At least one checkbox from fav[] group is required. fav[]组中至少需要一个复选框。

I used the errorPlacement option to move the error message before each grouping but you can adjust as needed. 我使用errorPlacement选项将错误消息移到每个分组之前,但是您可以根据需要进行调整。 See all plugin options . 查看所有插件选项

Working DEMO: http://jsfiddle.net/AsuyC/ 工作演示: http//jsfiddle.net/AsuyC/

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

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