【发布时间】:2015-06-02 15:50:56
【问题描述】:
我有一个脚本,它显示来自数组的问题,并使用两个按钮上的.click 事件从一个问题切换到下一个问题:上一个和下一个。当我加载页面时,$(":radio").change 选择器工作正常,但是当我单击上一个或下一个时它停止工作。
我尝试将$("#previous) 更改为console.log 以查看.click 是否是罪魁祸首,它似乎正在工作。我认为我的显示功能有问题,但我似乎无法弄清楚原因。
问题数组
var quizQuestions =
[{
question: "1. Who is Prime Minister of the United Kingdom?",
choices: ["David Cameron", "Gordon Brown", "Winston Churchill", "Tony Blair"],
correctAnswer:0
},
{
question: "2. Who is Prime Minister of the United Kingdom?",
choices: ["David Cameron", "Gordon Brown", "Winston Churchill", "Tony Blair"],
correctAnswer:2
},
{
question: "3. Who is Prime Minister of the United Kingdom?",
choices: ["David Cameron", "Gordon Brown", "Winston Churchill", "Tony Blair"],
correctAnswer:1
}];
JavaScript 代码 (jQuery)
$(document).ready(function(){
var all = quizQuestions,
q = $("#question"),
c = $("#choices"),
ans = [],
current = 0;
display(current);
function display (id) {
var question = all[id].question;
var choices = all[id].choices;
q.html(question);
c.html(function(){
var output = "<form>";
for (var i = 0; i < choices.length; i++) {
output += "<input type='radio' name='question" +id
+ "' value='" + choices[i] + "'> "
+ choices[i] + "<br>";
};
output += "</form>"
// console.log(output);
return output;
});
};
function changeDisplay (dir) {
if (dir == 'next' && current < all.length-1) current++;
if (dir == 'prev' && current > 0) current--;
display(current);
}
function answerQ (q, a) {
ans[q] = a;
console.log(ans);
}
$(":radio").change(function(){
answerQ( $(this)[0].name, $(this).val() );
});
$("#previous").click(function (){ changeDisplay('prev'); });
$("#next").click(function (){ changeDisplay('next'); });
// console.log("all.length: " + all.length + " | ans: " + ans + " | current: " + current);
});
【问题讨论】:
-
有一个
console.log我无法在小提琴上显示。它应该显示所选问题/答案的name和value。
标签: javascript jquery