简体   繁体   English

使用jQuery检查单选按钮的值并更改CSS类

[英]Checking value of radio buttons with jQuery and changing CSS class

I'm working on a 100 question multiple choice quiz (it displays 20 questions on five tabs). 我正在进行100个问题的多项选择测验(它在五个标签上显示20个问题)。 It's fine if folks can see the answers with the page source - this is just practice. 如果人们可以通过页面源查看答案,这很好 - 这只是练习。

What I currently have is a php array (currently with only 5 "test" questions) that displays the row with the question, four radio buttons and a submit button. 我目前拥有的是一个php数组(目前只有5个“测试”问题),它显示带有问题的行,四个单选按钮和一个提交按钮。 The idea is the user reads the question, selects an answer and submits. 想法是用户阅读问题,选择答案并提交。 If the answer is right, the radio button appears green. 如果答案是正确的,单选按钮显示为绿色。 If it is wrong, the radio button turns red and the user tries again. 如果错误,单选按钮变为红色,用户再次尝试。

Here is the main portion of my PHP code: 这是我的PHP代码的主要部分:

<?php
$set = array();

$set[] = array('q'=>'Question1','qurl'=>'001','ans'=>'zhidao','a'=>'zhidao','b'=>'zhidou','c'=>'zhedao','d'=>'zhedau');
$set[] = array('q'=>'Question2','qurl'=>'002','ans'=>'dajia','a'=>'dazha','b'=>'dajia','c'=>'taqia','d'=>'tazha');
$set[] = array('q'=>'Question3','qurl'=>'003','ans'=>'boduo','a'=>'bodou','b'=>'buoduo','c'=>'boduo','d'=>'buodo');
$set[] = array('q'=>'Question4','qurl'=>'004','ans'=>'wanhuan','a'=>'wanchuan','b'=>'wanhuan','c'=>'wanchun','d'=>'wachuan');
$set[] = array('q'=>'Question5','qurl'=>'005','ans'=>'zhangkou','a'=>'zhongkou','b'=>'zhangko','c'=>'zhangkuo','d'=>'zhangkou');

shuffle($set);
?>

   <form>
                <table>
                                                                                <tr class="q0">                     
                        <td><td><a class="question" href="#">Question goes here</a></td></td>
                        <td><input class="radiostyle" type="radio" name="answers" value ="zhidao" /> zhidao</td>
                        <td><input class="radiostyle" type="radio" name="answers" value ="zhidou" /> zhidou</td>
                        <td><input class="radiostyle" type="radio" name="answers" value ="zhedao" /> zhedao</td>
                        <td><input class="radiostyle" type="radio" name="answers" value ="zhedau" /> zhedau</td>
                        <td><input class="submit submit-0" type="submit" value="Submit" /></td>
                    </tr>

Here is my jQuery: 这是我的jQuery:

function checkAnswer(set) {
if (set == 2) { /* if this is question set 2 out of the 5 kinds of questions */
        $('#questions .submit').click(function(e){
            var q = $(this).attr('class').split(' ')[1].split('-')[1];
            $('#questions .q'+q+' input').removeClass('radiowrong');
            var ans = $("input[@name=answers]:checked").val();
            if (ans == jset[q]['ans']) {
                $('#questions .q'+q+' .radiostyle').addClass('radioright');
                alert('correct!');}
            else {
                $('#questions .q'+q+' .radiostyle').addClass('radiowrong');}

            e.preventDefault();
        });
    }
}

Now, I have not worked a lot with jQuery so please consider me a beginner. 现在,我没有用jQuery做过很多工作,所以请认为我是初学者。 If my code looks inefficient, my apologies. 如果我的代码看起来效率低下,我很抱歉。 I have two main questions about my code: 关于我的代码,我有两个主要问题:

First, I'm having a hard time getting my checkAnswer function to work correctly. 首先,我很难让checkAnswer功能正常工作。 I inserted an alert to see if my if logic is correct, but I have not been successful. 我插入一个警报,看看我的if逻辑是否正确,但我没有成功。 Any suggestions on how I can get this to work? 有关如何使其工作的任何建议?

Second, how can I modify my jQuery so I can properly change the class of the radio button? 其次,我如何修改我的jQuery,以便我可以正确更改单选按钮的类? Again, I'm looking to make the radio button green if the user selects the correct answers and red if they are wrong. 再次,如果用户选择正确的答案,我希望将单选按钮设为绿色,如果错误则为红色。

(Please know: If there is an easier way to show the user if they are either right or wrong-like changing the text color of the answer they select-I'm all ears. Really anything will suffice.) (请注意:如果有一种更简单的方式向用户显示他们是对还是错 - 比如改变他们选择的答案的文字颜色 - 我的耳朵都是。真的什么都足够了。)

If I need to provide more information, please let me know. 如果我需要提供更多信息,请告诉我。

Many many thanks. 非常感谢。

First of all, this is a working example of the solution - jsfiddle.net/avrelian/hrnDG/2/ . 首先,这是解决方案的一个工作示例 - jsfiddle.net/avrelian/hrnDG/2/

Below is a simplified version of your markup. 以下是标记的简化版本。 You may see that I am using label tag here. 你可能会看到我在这里使用label It is more user-friendly, since a user can simply click the label to check the radio button. 它更加用户友好,因为用户只需单击标签即可检查单选按钮。 Besides, label is much simpler to style (eg, on Mac OS). 此外,标签更加简单(例如,在Mac OS上)。 I am using "right" and "wrong" classes to mark appropriate answer elements. 我使用“正确”和“错误”类来标记适当的答案元素。

<form>
<table>
<tr class="q1">                       
    <td><a class="question" href="#">What is my favorite language?</a></td>
    <td>
        <input class="radiostyle wrong" type="radio" name="answers" value="Java" id="answers_1_Java" />
        <label for="answers_1_Java">Java</label></td>
    <td>
        <input class="radiostyle right" type="radio" name="answers" value="JavaScript" id="answers_1_JavaScript" />
        <label for="answers_1_JavaScript">JavaScript</label></td>
    <td>
        <input class="radiostyle wrong" type="radio" name="answers" value="Ruby" id="answers_1_Ruby" />
        <label for="answers_1_Ruby">Ruby</label></td>
    <td>
        <input class="radiostyle wrong" type="radio" name="answers" value="C#" id="answers_1_C#" />
        <label for="answers_1_C#">C#</label></td>
    <td><input class="submit submit-1" type="submit" value="Submit" /></td>
    <td></td></tr></table></form>​

Let us extract presentation logic into CSS. 让我们将表示逻辑提取到CSS中。

input.right.checked + label {
    color: green;
}

input.wrong.checked + label {
    color: red;
}

And, finally, let us set a handler for submit button. 最后,让我们为submit按钮设置一个处理程序。

$('.submit').click(function() {
    $(this).closest('tr')
        .find('[name="answers"]')
            .removeClass('checked')
        .filter(':checked')
            .addClass('checked');
    return false;
});​

NB: The solution can be even easier with CSS3 :checked pseudo-class, since you have no need in submit buttons. 注意:使用CSS3 :checked伪类可以更轻松地解决方案,因为您不需要提交按钮。 Please, see example - jsfiddle.net/avrelian/hrnDG . 请参见示例 - jsfiddle.net/avrelian/hrnDG

input.right:checked + label {
    color: green;
}

input.wrong:checked + label {
    color: red;
}

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

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