开发者

jquery Radio input validation

    <form id="v-1">
        <input type="radio" name="ot-1" value="o-1" /> answer1
        <input type="radio" name="ot-1" valu开发者_C百科e="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?


<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>


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;
    }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜