我想写一个测验,但一旦一个答案是正确的,这个问题的答案的按钮也证明是正确的,当有另外一个问题。我已经竭尽所能来解决它,但我只是没有一个线索是什么问题。。点击()不更新按钮状态
的jsfiddle:http://jsfiddle.net/bz6v5nbv/1/
错误重建:以第一个问题答案C(正确),并再次C(此时它实际上B的)在第二个。即使B是正确的,点击时C也是绿色的。
$(document).ready(function() {
var q = [];
q[1] = [3, "1", "Musik", "Welches Hotel ist sehr musikalisch?", "Hotel California",
"Riu Hotel", "Tokio Hotel", "Hotel Mama"];
q[2] = [2, "1", "Musik", "Was sitzt in einer Konservendose, singt und liest Nachrichten vor?",
"ein Schwammoli", "ein Radioli", "ein Tivoli", "ein Tivoli"];
q[3] = [4, "1", "Musik", "dd",
"ein Schwammoli", "ein Radioli", "ein Tivoli", "ein Tivoli"];
var fill = function(data) {
//buttons get filled with data from the array
$("#number").html(data[1]);
$("#cat").html(data[2]);
$("#ques span").html(data[3]);
$("#answ .answ:nth-child(1) button").html(data[4]);
$("#answ .answ:nth-child(2) button").html(data[5]);
$("#answ .answ:nth-child(3) button").html(data[6]);
$("#answ .answ:nth-child(4) button").html(data[7]);
$("#answ .answ:nth-child(" + data[0] + ") button").attr("data-state", "true");
//images are set, depending on the true/false state of the button
$("#answ .answ button").each(function() {
$(this).click(function() {
var button = $(this);
$(this).css("background-image", "url(images/btnBgLogged.png)");
$(this).css("border-image-source", "url(images/btnLogged.png)");
button.click(function() {
if (button.data("state") == true) {
button.css("background-image", "url(images/btnBgTrue.png)");
button.css("border-image-source", "url(images/btnTrue.png)");
} else {
button.css("background-image", "url(images/btnBgFalse.png)");
button.css("border-image-source", "url(images/btnFalse.png)");
}
setTimeout(next, 3000);
});
});
})
}
var clear = function() {
$("#answ .answ:nth-child(1) button").removeAttr("style");
$("#answ .answ:nth-child(2) button").removeAttr("style");
$("#answ .answ:nth-child(3) button").removeAttr("style");
$("#answ .answ:nth-child(4) button").removeAttr("style");
$("#answ .answ:nth-child(1) button").removeAttr("data-state");
$("#answ .answ:nth-child(2) button").removeAttr("data-state");
$("#answ .answ:nth-child(3) button").removeAttr("data-state");
$("#answ .answ:nth-child(4) button").removeAttr("data-state");
}
var count = 1;
function next() {
clear();
fill(q[count]);
count++;
}
next();
});
你的jsfiddle链接似乎无法正常工作,您可以修复它来帮你吗? – Jaay 2015-02-24 14:51:18
你已经描述了你想要避免的行为,但是你忘记描述你想要的行为。无论哪种方式,绑定一个新的点击处理程序,每次点击*可能不是你想要的* – blgt 2015-02-24 14:53:28