if 语句内的 for 循环 javascript

有没有办法在循环中执行 !radios[0].checked && !radios[1].checked && !radios[2].checked 并有时保持 if 条件不变?我的代码是:


    function afterLogin(event){

        event.preventDefault(); 

        if((!radios[0].checked && !radios[1].checked && !radios[2].checked) || userName.value === "" || numOfQuestions.value === ""){

            skillMsg.innerHTML = "Please check one of the skills before login";

            isTrue = false;

        }else{

            title[0].innerHTML = "Welcome " + userName.value;

            loginForm.style.display = "none";

            calForm.style.display = "block";

            formsColor.style.backgroundColor  = color.value;

            startTheGame();

            isTrue = true;

        }

        

    }

这就是我尝试做的:


    function afterLogin(event){

        event.preventDefault(); 

        for(let i = 0; i < radios.length; i++){

            if(!radios[i].checked || userName.value === "" || numOfQuestions.value === ""){

                //change the plase here

                skillMsg.innerHTML = "Please check one of the skills before login";

                return false;

            }else{

                title[0].innerHTML = "Welcome " + userName.value;

                loginForm.style.display = "none";

                calForm.style.display = "block";

                formsColor.style.backgroundColor  = color.value;

                //return true;

                startTheGame();

                //return true;

                

            }

        

        }

    }


慕标琳琳
浏览 128回答 3
3回答

一只甜甜圈

尝试这个 ?您可以使用Array.every可用的方法。function afterLogin(event){    event.preventDefault();     if((!radios.every(radio => radio.checked) || userName.value === "" || numOfQuestions.value === ""){        skillMsg.innerHTML = "Please check one of the skills before login";        isTrue = false;    }else{        title[0].innerHTML = "Welcome " + userName.value;        loginForm.style.display = "none";        calForm.style.display = "block";        formsColor.style.backgroundColor  = color.value;        startTheGame();        isTrue = true;    }    }

DIEA

是的,有可能,创建一个不同的验证函数来进行健全性检查,或者在同一个函数本身中进行&nbsp; &nbsp; function afterLogin(event){&nbsp; &nbsp; &nbsp; &nbsp; event.preventDefault();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; let notChecked = false;&nbsp; &nbsp; &nbsp; &nbsp; for(let i = 0; i < radios.length; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!radios[i].checked){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; notChecked = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(notChecked || userName.value === "" || numOfQuestions.value === ""){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; skillMsg.innerHTML = "Please check one of the skills before login";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isTrue = false;&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title[0].innerHTML = "Welcome " + userName.value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loginForm.style.display = "none";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; calForm.style.display = "block";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; formsColor.style.backgroundColor&nbsp; = color.value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startTheGame();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isTrue = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; }

波斯汪

是的,您可以使用 every() 函数来检查这一点。var radios = [{checked: false}, {checked: false}, {checked: false}, {checked: false}];if(radios.every(checkValue) || userName.value === "" || numOfQuestions.value === "") {// your code}function checkValue({checked}) {&nbsp; return checked === false;}还要尝试使用干净的代码原则。就像尝试使用“失败优先”方法一样,使用 if 并返回,这样您就不必在那里使用 else 块。只需将 else 部分放在 if 块之外即可。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript