简体   繁体   English

使用 javascript 或 css 单击时禁用单选按钮并更改颜色

[英]Disable radio button and change the color when click using javascript or css

I need to change the color of radio button on click.我需要在单击时更改单选按钮的颜色。 There are four options which the user can choose from.有四个选项可供用户选择。 When a user chooses an option, i would like the other radio buttons to disable and change the color like for wrong answer 'red' color and for right answer 'green' color.当用户选择一个选项时,我希望其他单选按钮禁用并更改颜色,例如错误答案“红色”颜色和正确答案“绿色”颜色。 I have done it to the point of disabling the radio buttons.我已经做到了禁用单选按钮。 The answer is "Prakash Tandon"答案是“Prakash Tandon”

 function myFunction() { document.getElementById("ritema").disabled = true; document.getElementById("ritemb").disabled = true; document.getElementById("ritemc").disabled = true; document.getElementById("ritemd").disabled = true; }
 1. Author of the book 'Punjabi Century' <br/> <input id="ritema" name="ritem" type="radio" value="ropt1" onclick="myFunction()" /> <label for="ritema">Goivind Nihalani</label><br> <input id="ritemb" name="ritem" type="radio" value="ropt2" onclick="myFunction()" /> <label for="ritemb">Narayan Bharathi</label> <div class="radio-item-false"> <input onclick="myFunction()" id="ritemc" name="ritem" type="radio" value="ropt3" /> <label for="ritemc">Urvashi Bhatalia</label> </div> <div class="radio-item-true"> <input onclick="myFunction()" id="ritemd" name="ritem" type="radio" value="ropt4" /> <label for="ritemd">Prakash Tandon</label> </div>

You can check your answers in JavaScript and highlight options accordingly.您可以在 JavaScript 中检查您的答案并相应地突出显示选项。

By assuming "Prakash Tandon" is right answer, I have written the script, but it needs to be enhanced.通过假设“Prakash Tandon”是正确的答案,我已经编写了脚本,但它需要增强。

 function myFunction(obj) { document.getElementById("ritema").disabled = true; document.getElementById("ritemb").disabled = true; document.getElementById("ritemc").disabled = true; document.getElementById("ritemd").disabled = true; if (obj.labels[0].innerText == "Prakash Tandon") { obj.labels[0].style.background = "green"; } else { obj.labels[0].style.background = "red"; } }
 <input id="ritema" name="ritem" type="radio" value="ropt1" onclick="myFunction(this)" /> <label for="ritema">Goivind Nihalani</label><br> <input id="ritemb" name="ritem" type="radio" value="ropt2" onclick="myFunction(this)" /> <label for="ritemb">Narayan Bharathi</label> <div class="radio-item-false"> <input onclick="myFunction(this)" id="ritemc" name="ritem" type="radio" value="ropt3" /> <label for="ritemc">Urvashi Bhatalia</label> </div> <div class="radio-item-true"> <input onclick="myFunction(this)" id="ritemd" name="ritem" type="radio" value="ropt4" /> <label for="ritemd">Prakash Tandon</label> </div>

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

相关问题 无法在materialize.css中使用javascript更改单击时的按钮颜色 - unable to change button color on click using javascript in materialize.css 如何使用 javascript 更改特定单选按钮选项的颜色? - How to Change the color of the particular radio button option using javascript? 单击时永久更改单选按钮的颜色 - Change color of a Radio button permanently on click 单击时使用多种颜色更改单选按钮颜色 - Change radio button color on click with multiple colors 我需要使用 HTML、CSS 和 Javascript 制作一个按钮,以便在单击按钮时将字体颜色从红色变为红色 - I need to make a button using HTML, CSS, and Javascript to change font color from to red when the button is clicked 有没有办法更改 JavaScript 中所选单选按钮的颜色? - Is there a way to change the color of the selected radio button in JavaScript? JavaScript-如果选中了单选按钮,请更改背景色 - JavaScript - If radio button is checked, change background color 使用 javascript 单击 li 时检查单选按钮 - check the radio button when click on the li using javascript 当我单击链接时如何使用javascript在CSS中更改图像中的选定颜色 - when i click link how to change selected color in image in css using javascript 如何使用 jquery/javascript 在 asp.net c# 中禁用单选按钮上的链接按钮单击 - How to disable a link button on radio button click in asp.net c# using jquery/javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM