我正在写一个jquery测验。
首先我根据页面设置变量:
if ($("body").hasClass("questionone")){
var $currentQuestion = 1;
console.log($currentQuestion);
}
else if ($("body").hasClass("questiontwo")){
var $currentQuestion = 2;
console.log($currentQuestion);
}
else if ($("body").hasClass("questionthree")){
var $currentQuestion = 3;
console.log($currentQuestion);
};
然后,当他们回答一个问题时,我会弹出一个说不正确或正确的弹出按钮以及一个说下一个问题的按钮。下一个问题按钮会根据当前的Question变量重定向。问题是它适用于if部分并从问题1重新定向到问题2关于点击但问题2它不会重新指向问题3.
以下是重定向代码:
$(".nextquestion").on("click", function(){
if( $currentQuestion = 1 ) {
window.location.href = "question2.html";
}
else if( $currentQuestion = 2 ) {
console.log("thisworks");
window.location.href = "question3.html";
}
else if( $currentQuestion = 3 ) {
window.location.href = "question4.html";
};
});
感谢您的帮助。
答案 0 :(得分:4)
将您的=
更改为==
或===
,这样您就不会分配,只需进行比较:
if( $currentQuestion === 1 ) {
...
else if( $currentQuestion === 2 ) {
...
else if( $currentQuestion === 3 ) {
请避免重复var
声明(var $currentQuestion = 2;
),这会使代码难以破译。
答案 1 :(得分:0)
if ($("#body").hasClass("questionone")){
var $currentQuestion = 1;
console.log($currentQuestion);
}
else if ($("#body").hasClass("questiontwo")){
var $currentQuestion = 2;
console.log($currentQuestion);
}
else if ($("#body").hasClass("questionthree")){
var $currentQuestion = 3;
console.log($currentQuestion);
};
$(".nextquestion").on("click", function(){
if( $currentQuestion == 1 ) {
window.location.href = "question2.html";
}
else if( $currentQuestion == 2 ) {
console.log("thisworks");
window.location.href = "question3.html";
}
else if( $currentQuestion == 3 ) {
window.location.href = "question4.html";
};
});
试试这个