【发布时间】:2021-07-30 16:28:44
【问题描述】:
我想在提交后检查salad 或side dish 是否未选中。
我有一个可行的方法,但它非常难看,我相信对于这样一个简单的任务有更简单的解决方案
我的方法:
function radiosChecker(){
let radioGroups = [];
let radios = document.querySelectorAll(`input[name^="${foodId}_"][type="radio"]`); // Get all radios first
for(let i = 0; i < radios.length; i++){ // Creating an array of elements each one of them representing its group
if(i == 0){
radioGroups.push(radios[i]);
continue;
}
if(i > 0){
if(radios[i].name != radios[i-1].name){
radioGroups.push(radios[i])
}
}
}
let okays = [];
radioGroups.forEach((ele)=>{
let group = document.querySelectorAll(`input[name="${ele.name}"]`); // Get all radios with same name
for(let i = 0;i < group.length; i++){ // loop untill u find one checked and append a flag to the okays
if(group[i].checked){
okays.push(true);
break;
}
}
})
if(radioGroups.length == okays.length){
return true;
}else{
return false;
}
}
【问题讨论】:
标签: javascript jquery performance dom radio-button