2

我目前正在学习 javascript,并尝试按照 javascript_source 中提供的示例在带有单选按钮的 javascript 中进行测验 我现在陷入了这样一种情况,即我希望将所选单选按钮标签颜色更改为红色或绿色选择的答案。

我已经把我的代码放在了 jsfiddle中,我可以让它在 ny 浏览器中工作,但我不知道为什么它在 jsfiddle 中似乎不起作用,基本思想是一样的,只是我想改变颜色根据正确或错误选择单选按钮标签。

任何有关这方面的帮助将不胜感激

* * EDIT * jsfiddle 现在可以工作了,这要归功于下面的评论

4

2 回答 2

3

修复它,http://jsfiddle.net/aEeKt/8/。我所做的只是添加以获取父级并根据答案为其<li>添加一个.correct或类。.incorrect

HTML

<h3>Web Design Quiz</h3>

<form name="quiz">
1. What does CSS stand for?
<ul style="margin-top: 1pt">
  <li><input type="radio" name="q1" value="Colorful Style Symbols">Colorful Style Symbols</li>
  <li><input type="radio" name="q1" value="Cascading Style Sheets">Cascading Style Sheets</li>
  <li><input type="radio" name="q1" value="Computer Style Symbols">Computer Style Symbols</li>
</ul>

2. What does DHTML stand for?
<ul style="margin-top: 1pt">
  <li><input type="radio" name="q2" value="Dramatic HTML">Dramatic HTML</li>
  <li><input type="radio" name="q2" value="Design HTML">Design HTML</li>
  <li><input type="radio" name="q2" value="Dynamic HTML">Dynamic HTML</li>
</ul>
3. Who created Javascript?
<ul style="margin-top: 1pt">
  <li><input type="radio" name="q3" value="Microsoft">Microsoft</li>
  <li><input type="radio" name="q3" value="Netscape">Netscape</li>
  <li><input type="radio" name="q3" value="Sun Micro Systems">Sun Micro Systems</li>
</ul>
4. What does CGI stand for?
<ul style="margin-top: 1pt">
  <li><input type="radio" name="q4" value="Cascading Gate Interaction">Cascading Gate Interaction</li>
  <li><input type="radio" name="q4" value="Common GIF Interface">Common GIF Interface</li>
  <li><input type="radio" name="q4" value="Common Gateway Interface">Common Gateway Interface</li>
</ul>

<input type="button" value="Get score" onClick="getScore(this.form)">
<input type="reset" value="Clear answers">
<p> Score = <strong><input class="bgclr" type="text" size="5" name="percentage" disabled></strong><br><br>
Correct answers:<br>
<textarea class="bgclr" name="solutions" wrap="virtual" rows="4" cols="30" disabled>
</textarea>
</form>

Javascript ​</p>

// Insert number of questions
var numQues = 4;

// Insert number of choices in each question
var numChoi = 3;

// Insert number of questions displayed in answer area
var answers = new Array(4);

// Insert answers to questions
answers[0] = "Cascading Style Sheets";
answers[1] = "Dynamic HTML";
answers[2] = "Netscape";
answers[3] = "Common Gateway Interface";

// Do not change anything below here ...

function getScore(form) {
    var score = 0;
    var currElt;
    var currSelection;
    for (i = 0; i < numQues; i++) {
    currElt = i * numChoi;
    for (j = 0; j < numChoi; j++) {
        currSelection = form.elements[currElt + j];
        cParent = currSelection.parentNode;
        if (currSelection.checked) {
        if (currSelection.value == answers[i]) {
            score++;
            cParent.className = 'correct';
            break;
        }else{
            cParent.className = 'incorrect';
        }
        }
    }
    }
    score = Math.round(score / numQues * 100);
    form.percentage.value = score + "%";
    var correctAnswers = "";
    for (i = 1; i <= numQues; i++) {
    correctAnswers += i + ". " + answers[i - 1] + "\r\n";
    }
    form.solutions.value = correctAnswers;
}​

CSS

li.correct{
    color: green;
}
li.incorrect{
    color: red;
}
​

​</p>

于 2012-10-01T07:51:58.547 回答
0

在 jsfiddle 中,左侧有一列包含各种设置。在“选择框架”下,有一个默认为“onLoad”的下拉框。这允许您选择如何在页面中包含和执行您的 javascript。

当您选择“onLoad”时,声明的任何函数或变量都不是全局的。但是,使用onClick="getScore(this.form)"要求函数是全局的。相反,您应该选择“无包裹(头部)”或“无包裹(主体)”。

tl;博士

Choose 'no wrap (head)' from the dropdown box on the left for your example to work in jsfiddle.

于 2012-10-01T07:53:49.367 回答