It sounds strange that value="2" would be the number of points.
Usually, a value would be used to indicate a unique answer for a
question. Usually, 1, 2, 3, 4 if you have 4 possible answers for one
question. I would suggest using the ID or CLASS attribute, or
Javascript objects to store that type of info.
Do you have another attribute that determines what the "correct"
answer is for a question? Or is there no correct answer? Just a
selection?

Otherwise, to do what you want to do, you can do something like
(untested):
(You might have to double-check this, but it's an idea.)

var total = 0;

$("#submit_btn").click(function() {
    $("input[name^='question_']).each(function() {
        var answer_value = $(':checked', this).val();
        if (answer_value)
            total += parseInt(answer_value);
    });
});

Question 1:
<input type="radio" name="question_1" value="1"> Answer 1
<input type="radio" name="question_1" value="2"> Answer 2
<input type="radio" name="question_1" value="3"> Answer 3
<input type="radio" name="question_1" value="4"> Answer 4

Question 2:
<input type="radio" name="question_2" value="1"> Answer 1
<input type="radio" name="question_2" value="2"> Answer 2
<input type="radio" name="question_2" value="3"> Answer 3
<input type="radio" name="question_2" value="4"> Answer 4

<input type="button" id="submit_btn" value="Submit">


On Jul 27, 9:41 am, briggs81 <brigg...@gmail.com> wrote:
> This is probably simple, but I am new to jQuery and am trying to wrap
> my head around things.
>
> Basically what I am trying to do is a very basic quiz. I have 3 groups
> of radio buttons (3 questions with 4 possible answers for each
> question, so.. multiple choice).
>
> Each answer has a number value assigned to it, i.e. value="2". This
> would be 2 points.
>
> After pressing a button I need to be able to select all the radio
> buttons have that been checked, and then add all their point values up
> and give the user a score.
>
> Any hints would be appreciated.

Reply via email to