我正在寻找一种从单选按钮中获取价值的方法。我这里有html。
<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>
这是Jquery代码。
$(document).ready (function () {
//
var answers =
[["Fee","Fi","Fo"],
["La","Dee","Da"]],
questions =
["Fee-ing?",
"La-ing?"],
corAns = ["Fee", "La"];
var counter = 0;
var correctAnswers = 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);
alert($('input[name="ans"]:checked').val())
//if ($('input[name=ans]:checked').val() == corAns[counter]) {
//correctAnswers++;
//}
}
else {
$facts.text('You are done with the quiz ' + correctAnswers);
$(this).hide()
}
counter++;
});
//
});
如您所知,它一直返回未定义,我不太确定我做错了什么。
顺便说一句,价值在于衡量这是我正在做的测验中的正确答案还是错误答案。所以,最终我需要array[counter]
在 if 语句中使用。但是,我把它拆开来测试和调整并弄清楚如何获得价值。