我正在尝试检查至少有一个单选按钮在页面上被检查,它们在不同的组中,所以我试图创建一个所有组的aray并运行它们但我似乎无法看到什么代码有问题,任何人都知道我做错了什么?
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;
}
答案 0 :(得分:0)
for (var i=0; i<chx.length; i++) {
for (var x=0; x< day.length;x++) {
if (chx[i].type == 'radio' chx[i].name == day[x] && chx[i].checked) {
return true;
}
}
}
答案 1 :(得分:0)
如果你有jQuery,它可能会更短一些。
类似的东西:
//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));
});