从Radio Button Jquery获取价值

时间:2013-10-29 02:16:38

标签: jquery jquery-selectors radio-button

我知道之前已经问过这个问题,但我似乎无法做到这一点。它始终返回未定义错误。

var currentAnswers = $('input[name="ans"]:checked').map(function() {
    return this.value;
}).get();

$(document).ready (function () {

//

var answers = 
[["Fee","Fi","Fo"], 
["La","Dee","Da"]],
questions = 
["Fee-ing?",
"La-ing?"],
corAns = ["Fee", "La"];

var counter = 0;

var $facts = $('#main_ .facts_div'),
$question = $facts.find('.question'),
$ul = $facts.find('ul'),
$btn = $('.myBtn');


$btn.on('click', function() {
    if (counter < questions.length) {
        $question.text(questions[counter]);

        var ansstring = $.map(answers[counter], function(value) {
            return '<li><input type="radio" name="ans" value="0"/>'
            + value + '</li>'}).join('');
        $ul.html(ansstring);

        var currentAnswers = $('input[name="ans"]:checked').map(function() {
            return this.val();
        }).get();

        var correct = 0;
        if (currentAnswers[counter]==corAns[counter]) {
            correct++;
        }


    }
    else {
        $facts.text('You are done with the quiz ' + correct);
        $(this).hide();
    }
        counter++;
});

//
});

这很长。我只是不知道我做错了什么。

 <div id="main_">
    <div class="facts_div">
        <span class="question"></span>
        <ul></ul>
    </div>
    <div id = "next_button">
    <form>
        <input id="x" type="button" class="myBtn" value="Press Me">
    </form>
    </div>
</div>
</div>

我觉得答案是愚蠢的。就像一个未声明的变量或其他东西,但我只是不知道。我很困惑。

0 个答案:

没有答案