Ilya Medvedev
Ilya Medvedev

Reputation: 1279

jquery Radio input validation

    <form id="v-1">
        <input type="radio" name="ot-1" value="o-1" /> answer1
        <input type="radio" name="ot-1" value="o-2" /> answer2
        <input type="radio" name="ot-1" value="o-3" /> answer3
        <input type="radio" name="ot-1" value="o-4" /> answer4
    </form>

How to make: if checked Answer 1, then in var test adds 1 point, if one of other Answers cheked, then adds 0 points?

Upvotes: 0

Views: 544

Answers (2)

Naveen Velaga
Naveen Velaga

Reputation: 646

In jquery you can use the following to get the value of a checked radio button

var value = $("input[@name=ot-1]:checked").val();

U can use this in a button event:

$('#submit_button').click(function() {
    var value = $("input[@name=ot-1]:checked").val();
    var test=0;
    if (value == "o-1") {
        test = 1;
    }
});

Upvotes: 1

rickyduck
rickyduck

Reputation: 4084

<form id="v-1">
    <input type="radio" name="ot-1" value="o-1" /> answer1
    <input type="radio" name="ot-1" value="o-2" /> answer2
    <input type="radio" name="ot-1" value="o-3" /> answer3
    <input type="radio" name="ot-1" value="o-4" /> answer4
</form>

JS:

<script>

    test = 0;
    $('input[name=ot-1]').click(function(){
        if($(this).val()=="o-1"){
            test=test+1;
        }
    });
</script>

Upvotes: 5

Related Questions