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));
});