如何更改无线电输入字段中文本的颜色

时间:2015-03-02 15:46:25

标签: javascript html

好的,我正在进行一项测试,其中包含一系列包含答案列表的问题。我试图让测试改变用户错误答案的颜色以突出显示红色并将正确答案突出显示为绿色。我理解如何更改元素的背景颜色。但是我不明白如何更改无线电类型的输入元素的内部html的背景颜色。当我运行此代码时,我认为它试图改变实际的无线电输入。任何帮助表示赞赏。

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>

我的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';

}

2 个答案:

答案 0 :(得分:2)

只需使两个css类“正确”和“错误”。然后将这些类分配给相应的li元素。

Jsfiddle demo

li.correct{

    color:green;
}
li.wrong{

    color:red;
}

答案 1 :(得分:2)

最好的方法是使用两个不同的类correctwrong并将它们分配给答案。

  1. 定义两个CSS类correctwrong的样式:

    .correct {
        background-color : green;
    }
    
    .wrong {
        background-color : red;
    }
    
  2. 然后在您的JavaScript中,为每个答案分配相应的类(相应的<li>标记):

    yourAnswer.className="correct"; or yourAnswer.className="wrong"; 
    
  3. 其中yourAnswer是每个所选答案的<li>

    注意:根据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