我试图弄清楚为什么,当我运行这段代码时,我的答案是正确的。
$(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 :(得分:0)
尝试
$(document).ready(function () {
//
var answers = [
["Fee", "Fi", "Fo"],
["La", "Dee", "Da"]
],
questions = ["Fee-ing?",
"La-ing?"],
corAns = ["Fee", "La"];
var counter, correct = 0;
var $facts = $('#main_ .facts_div'),
$question = $facts.find('.question'),
$ul = $facts.find('ul'),
$btn = $('.myBtn');
$btn.on('click', function () {
if (counter >= 0) {
var ans = $('input[name="ans"]:checked').val();
if (!ans) {
alert('please select an answer');
return;
}
console.log(ans, counter, corAns[counter])
if (ans == corAns[counter]) {
++correct;
}
counter++;
} else {
counter = 0;
}
if (counter < questions.length) {
$question.text(questions[counter]);
var ansstring = $.map(answers[counter], function (value) {
return '<li><input type="radio" name="ans" value="' + value + '"/>' + value + '</li>'
}).join('');
$ul.html(ansstring);
} else {
$facts.text('You are done with the quiz ' + correct);
$(this).hide();
$('#correct').text()
}
});
//
});
演示:Fiddle