[英]How do I fix this Illegal break statement?
I have a loop running through every radio. 我有一个循环贯穿每个收音机。 Based on the radio selected, the user earns a certain amount of points to their score.
基于所选择的无线电,用户获得一定数量的分数。 Once that score is 8 or higher, i have a display box that I want to show.
一旦得分为8或更高,我有一个我想要显示的显示框。 Problem is I only want it to display once, not for ever single instance of a score >= 8.
问题是我只希望它显示一次,而不是任何一个得分> = 8的单个实例。
for (var i = 0; i < allRadiosArr.length; i++) {
allRadiosArr[i].addEventListener('change', (e)=>{
totalScoreMain = 0;
let checkedRadios = document.querySelectorAll('input[type="radio"]:checked');
checkedRadios.forEach(function(checkedRadio){
totalScoreMain += parseInt(checkedRadio.getAttribute('value'));
});
if (totalScoreMain >= 8) {
referralModalMain.style.display = 'block';
break;
}
});
closeButtonMain.addEventListener('click', (e)=>{
referralModalMain.style.display = 'none';
});
}
You don't have to break;
你不必
break;
. 。 Use a flag, eg
let modalShown = false
, then set that to true
when you show the modal the first time, and don't show it again. 使用一个标志,例如
let modalShown = false
,然后在第一次显示模态时将其设置为true
,并且不再显示它。
let modalShown = false;
for (var i = 0; i < allRadiosArr.length; i++) {
allRadiosArr[i].addEventListener('change', (e)=>{
totalScoreMain = 0;
let checkedRadios = document.querySelectorAll('input[type="radio"]:checked');
checkedRadios.forEach(function(checkedRadio){
totalScoreMain += parseInt(checkedRadio.getAttribute('value'));
});
if (!modalShown && totalScoreMain >= 8) {
referralModalMain.style.display = 'block';
modalShown = true;
}
});
closeButtonMain.addEventListener('click', (e)=>{
referralModalMain.style.display = 'none';
});
}
Edit: Please make sure you read and understood all the helpful comments you received under your question, because they are more valuable than my simple answer and contain important advices. 编辑:请确保您阅读并理解您在问题中收到的所有有用的评论,因为它们比我的简单答案更有价值,并包含重要建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.