简体   繁体   English

如何更改单选输入字段中文本的颜色

[英]How do i change the color of the text in a radio input field

Ok so I am making a test that has a list of questions that contains a list of answers. 好的,所以我正在做一个测试,该测试的问题列表包含答案。 Im trying to make the test change the color of the users wrong answers to highlight red and have the correct answers highlighted green. 我试图使测试改变用户的颜色,错误答案以红色突出显示,而正确答案以绿色突出显示。 I understand how to change the background color of elements. 我了解如何更改元素的背景颜色。 However i do not understand how to change the background color of the inner html of a input element of the radio type. 但是我不明白如何更改单选类型的输入元素的内部html的背景颜色。 When i run this code i think its trying to change the actual radio input. 当我运行这段代码时,我认为它试图更改实际的无线电输入。 Any help is appreciated. 任何帮助表示赞赏。

A sample for HTML test: HTML测试示例:

<ol>

<li ><p id = "question 1">What are the three main areas of the Standard User Interface?</p>
  <ul type="none">
    <li><input type="radio" name="q1" value="0"  /> Header, Banner, Frame, Application Window</li>
    <li><input type="radio" name="q1" value="0"  /> Content Frame, Homepage, Form </li>
    <li><input type="radio" name="q1" value="1"  /> Application Navigator, Banner Frame, Content Frame </li>
    <li><input type="radio" name="q1" value="0"  /> Larry, Moe, and Curly</li>
  </ul>
</li>

<li><p id = "question 2">In the  User interface, what is the gray toolbar called which allows you to add bookmarks?</p>

  <ul type="none">
    <li><input type="radio" name="q2" value="0" /> Gauge</li>
    <li><input type="radio" name="q2" value="1" /> Edge</li>
    <li><input type="radio" name="q2" value="0" /> Remedy</li>
    <li><input type="radio" name="q2" value="0" /> Banner</li>
  </ul>
</li>

<li><p id = "question 3">What can be captured in an update set?</p>

  <ul type="none">
    <li><input type="radio" name="q3" value="0" /> Modified CI Rules</li>
    <li><input type="radio" name="q3" value="1" /> Business Rules</li>
    <li><input type="radio" name="q3" value="0" /> Scheduled Jobs</li>
    <li><input type="radio" name="q3" value="0" /> None of these</li>
  </ul>
</li>

</ol>

<button id = "finish" onchange = "hide" onclick="finishTest()"> Submit Test  </button> <button id = "retry"  onclick="reloadPage()"> Try Again?</button>

My javascript Code: 我的JavaScript代码:

function finishTest(){
//There are actually 37 questions on the test so far only included 3
var score = 0;
var totalQuestions = 37;


for(var questionNum = 1; questionNum<=totalQuestions; questionNum++) {
    var radios = document.getElementsByName('q'+questionNum);
    var uQuestion = document.getElementById("question "+questionNum).innerHTML;
    for (var i = 0, length = radios.length; i < length; i++) {
        if (radios[i].checked && radios[i].value=="1"){

            score++;
            alert(radios.innerHTML);
            radios.innerHTML.style.backgroundColor = "lawngreen";

            }else if (radios[i].checked && radios[i].value=="0"){


            alert(radios.innerHTML);
            radios.innerHTML.style.backgroundColor = "red";


            }
    }
}

score = parseFloat(score*100/totalQuestions).toFixed(1);
alert("You scored "+score+"%");

document.getElementById('finish').style.visibility='hidden';

}

Just make two css classes "correct" and "wrong". 只需使两个CSS类“正确”和“错误”即可。 Then assign these classes to the corresponding li elements. 然后将这些类分配给相应的li元素。

Jsfiddle demo Jsfiddle演示

li.correct{

    color:green;
}
li.wrong{

    color:red;
}

The best approach is to use two different classes correct and wrong and assign them to answers. 最好的方法是使用correctwrong两个不同的类,并将它们分配给答案。

  1. Define the style of two CSS classes correct and wrong : 定义两个CSS类的样式correctwrong

     .correct { background-color : green; } .wrong { background-color : red; } 
  2. Then in your JavaScript, assign the corresponding classes for each answer (the corresponding <li> tag) : 然后在您的JavaScript中,为每个答案分配相应的类(相应的<li>标记):

     yourAnswer.className="correct"; or yourAnswer.className="wrong"; 

Where yourAnswer is the <li> of each selected answer. 其中yourAnswer是每个选定答案的<li>

Note: As suggested by Chris Baker it would be better to put the input button and its corresponding text in a label together, it's more ergonomic as you can see in the first radio of the DEMO. 注意:Chris Baker所建议的那样,最好将input按钮及其对应的text放到label ,这更符合人体工程学,就像在DEMO的第一个收音机中看到的那样。

  <li>
    <label for="q1a">
        <input id="q1a" type="radio" name="q1" value="0"  /> Answer with a label
    </label>
  </li>

Here's a DEMO Fiddle 这是一个DEMO小提琴

暂无
暂无

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

相关问题 如何根据 onclick 更改输入字段背景颜色? - How do i change input field background color based on onclick? 当我输入并提交内容时,如何停止输入表单以更改背景颜色和文本颜色? - How do I stop the input form to change background color and text color when I input and submit something? 如何使用默认值更改输入字段中的文本值 - How do I change the text value in an input field with a default value 使用单选按钮更改输入文本的背景颜色 - Change input text bg color with radio button 选中分配给它的单选按钮时,如何使输入/文本字段为必填字段? - How do I make an input/text field mandatory when the radio button assigned to it is checked? Javascript:当选中/选中分配给它的单选按钮时,如何使输入/文本字段为必需字段? - Javascript: How do I make an input/text field mandatory(required) when the radio button assigned to it is checked/selected? 输入字段接收文本时如何更改标签颜色? - How to change label color when input field receives text? 输入文本时如何更改输入字段的背景色? - How to change the background color of an input field when text is entered? 如何在聚焦文本输入字段时更改 div 的背景颜色? - How to change the background color of the div while focusing the text Input field? 如果提交了某个值,如何更改输入类型 =“文本”中占位符文本的颜色? - How do i change the color of the placeholder text in an input type=“text” if a certain value is submitted?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM