验证多个复选框组 [英] Validate multiple checkbox groups

查看:93
本文介绍了验证多个复选框组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3组复选框,属于同一组的复选框具有相同的名称,但ID不同。
我需要检查每个组至少选择一个复选框。
我无法写正确的JavaScript代码。

你能帮我吗?

I have 3 groups of checkboxes, checkboxes that belong to the same group have the same name but a different id. I need to check that at least one checkbox is selected for each group. I can't write the correct javascript code.
Can you help me?

推荐答案

如果正确理解,此函数将执行:

If understood you correctly, this function will do:

function checkCheckBoxGroup(groupName) {
  var g = document.getElementsByName(groupName);

  for(var i = 0;i<g.length;i++) {
    if (g[i].checked) {
      return true;
    }
  }
  return false;
}

返回 false 如果没有选中复选框,并且选中了一个或多个复选框,则 true

It returns false if no checkbox was checked and true if one or more are checked.

这篇关于验证多个复选框组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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