简体   繁体   English

如何 select 单选按钮单击更改颜色

[英]How to select radio buttons with click to change color on click

I am trying to make a math quiz game for my classroom.我正在尝试为我的教室制作一个数学问答游戏。 I have created a page to select the type of game the user wants.我已经创建了一个页面到 select 用户想要的游戏类型。 I have created a function to select the the element when it is clicked to change colors but it is not changing colors.我已经创建了一个 function 到 select 元素,当它被单击以更改 colors 但它没有更改 Z99938413ACE7928 I have tried to hard code the styling and it works properly but I only want the styling when the radio container is selected.我试图对样式进行硬编码并且它可以正常工作,但我只想要在选择无线电容器时的样式。

 const startForm = document.getElementById('start-form'); const radioContainers = document.querySelectorAll('.radio-container'); const radioInputs = document.querySelectorAll('input'); const bestScores = document.querySelectorAll('.best-score-value'); startForm.addEventListener('click', () => { radioContainers.forEach((radioEl) => { radioEl.classList.remove('selected-label'); if (radioEl.childeren[1].checked) { radioEl.classList.add('selected-label'); } }); });
 RADIO CONTAINER TO BE SELECTED <div class="radio-container"> <label for="value-99">99 Questions</label> <input type="radio" name="Questions" value="99" id="value-99"> <span class="best-score"> <span>Best Score</span> <span class="best-score-value">0.0</span> </span> </div>

I think you mean this我想你的意思是这个

Delegation from startForm is fine, then we can loop over the divs to see if the radio in the div was checked startForm 的委托很好,然后我们可以遍历 div 来查看 div 中的 radio 是否被选中

 const startForm = document.getElementById('start-form'); const radioContainers = document.querySelectorAll('.radio-container'); startForm.addEventListener('click', (e) => { const tgt = e.target; if (tgt.name === "Questions") { const parent = tgt.closest('.radio-container') radioContainers.forEach(container => container.classList.toggle('selected-label', container === parent && tgt.checked)) } });
 .selected-label { color: green }
 <form id="start-form"> <div class="radio-container"> <label for="value-99">99 Questions</label> <input type="radio" name="Questions" value="99" id="value-99"> <span class="best-score">Best Score</span> <span class="best-score-value">0.0</span> </div> <div class="radio-container"> <label for="value-99">99 Questions</label> <input type="radio" name="Questions" value="99" id="value-99"> <span class="best-score">Best Score</span> <span class="best-score-value">0.0</span> </div> </form>

Question is somewhat a duplicate.问题有点重复。 change label color radio when checked I got the answer from here, and it can be done using CSS without needing to use JavaScript. 检查时更改 label 彩色收音机我从这里得到了答案,可以使用 CSS 完成,而无需使用 JavaScript。
(Notice some buttons have different colors when clicked) Here's a snippet (请注意,某些按钮在单击时具有不同的 colors)这是一个片段

 input[type="radio"]:checked ~ span { color:red; } input[type="radio"]:checked.correct ~ span { color:green; }
 <form id="start-form"> <div class="radio-container"> <label for="value-99">99 Questions</label> <input type="radio" name="Questions" value="99" id="value-99"> <span class="best-score">Best Score</span> <span class="best-score-value">0.0</span> </div> <div class="radio-container"> <label for="value-99">99 Questions</label> <input type="radio" name="Questions" value="99" id="value-99" class = "correct"> <span class="best-score">Best Score</span> <span class="best-score-value">0.0</span> </div> <div class="radio-container"> <label for="value-99">99 Questions</label> <input type="radio" name="Questions" value="99" id="value-99"> <span class="best-score">Best Score</span> <span class="best-score-value">0.0</span> </div> <div class="radio-container"> <label for="value-99">99 Questions</label> <input type="radio" name="Questions" value="99" id="value-99" class = "correct"> <span class="best-score">Best Score</span> <span class="best-score-value">0.0</span> </div> </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM