复选框验证 - 至少选择了一个 [英] Checkbox validation - at least one selected

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

问题描述

我有多个复选框,另有一个全选复选框

I have number of checkboxes and another checkbox for "Select All"

我想检查用户是否至少选择了一个复选框。需要在javascript中修改

I want to check if the user has selected at least one checkbox. Need modification in javascript

<script language="Javascript">

function doSubmit(){
function check_checkboxes() 
{ 
checked=false;
var c = document.getElementsByTagName('INPUT'); 
for (var i = 1; i < c.length; i++) 
{ 
    if (c[i].type == 'checkbox') 
    { 
    if (c[i].checked) {
    return true} 
    else {alert("Please identify what warehouses comply:");  }
        } 
    } //if I place my struts action here..its not working?
}    
document.holiDay.command.value= 'addingApp'; //My Struts action if something checked. 
document.holiDay.submit(); 
}    


推荐答案

var all=document.getElementById('holiDay');

在HTML ID中应该是唯一的,因此getElementById只会返回1个元素。也许您可以尝试getElementsByTagName - http://msdn.microsoft .com / en-us / library / ms536439(VS.85).aspx

In HTML IDs should be unique, so getElementById will only return 1 element. Perhaps you could try getElementsByTagName - http://msdn.microsoft.com/en-us/library/ms536439(VS.85).aspx ?

有点像...

function check_checkboxes()
{
  var c = document.getElementsByTagName('input');
  for (var i = 0; i < c.length; i++)
  {
    if (c[i].type == 'checkbox')
    {
       if (c[i].checked) {return true}
    }
  }
  return false;
}

并将您的Validate函数更改为...

and change your Validate function to...

function Validate()
{
    if(!check_checkboxes())
    {
        alert("Please identify what warehouses comply:");  
        return false;
    }
    return true;
}

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

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