javascript - 检查页面上是否至少选择了一个单选按钮 [英] javascript - check at least one radio button is selected on the page

查看:81
本文介绍了javascript - 检查页面上是否至少选择了一个单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在检查是否至少有一个单选按钮在页面上被选中,它们在不同的组中,所以我试图创建一个所有组的aray并运行它们,但我似乎无法看到什么代码有错误的人知道我做错了什么?

i am trying to check that at least one of my radio buttons are checked on the page, they are in different groups so i have tried to create an aray of all the groups and run therough them but i cant seems to see what is wrong with the code anyone know what i am doing wrong ?

function atLeastOneRadio() { 
      var chx = document.getElementsByTagName('input');
      var day = new array();
      day[0] = "monday";
      day[1] = "tuesday";
      day[2] = "wednesday";
      day[3] = "thursday";
      day[4] = "friday";
      for (var i=0; i<chx.length; i++) {
        if (chx[i].type == 'radio' chx[i].name == day[i] && chx[i].checked) {
          return true;
        } 
      }
      return false;
    }


推荐答案

类似于:

Something like:

//returns number of elements checked
$("input:checked").size();


//if you need to check if the name of the element is equal to something
$("input:checked").each(function(i){
    return($.inArray(this.name, days));
});

这篇关于javascript - 检查页面上是否至少选择了一个单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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