我在嘗試編碼測驗,根據選擇的答案將重定向到兩個輔助測驗之一。我有下面的代碼,我認爲它會工作,但它或者完成其他返回或重新加載測驗。輔助測驗位於ques2.html內,第三個位於ques3.html內。有沒有人有任何想法我做錯了,或有任何潛在的解決方案?JavaScript測驗重定向問題
var data = {
questions: [{
question: "Which interests you most?",
options: [ "Medicine" , "Physics" ]
}, {
question: "Which interests you most?",
options: [ "Vet/Animals" , "Astrology"]
}, {
question: "Which interests you most?",
options: [ "Nutrition" , "Chemistry"]
}, {
question: "Which interests you most?",
options: [ "Psychology" , "Forensic Science" ]
}, {
question: "Which interests you most",
options: [ "Dentistry" , "Biology"]
}],
};
var qIndex = 0,
index = 0,
aString = "",
maxQ = 5,
q = document.getElementById('ques'),
opt1 = document.getElementById('opt1'),
opt2 = document.getElementById('opt2');
function generate(){
q.innerHTML= data.questions[qIndex].question;
opt1.value= data.questions[qIndex].options[0];
opt2.value= data.questions[qIndex].options[1];
}
function optionClick(ans) {
if(qIndex%1==0){
aString+=ans;
index++;
}
qIndex++;
if(qIndex == maxQ){
redirect(generateResult);
} else{
generate();
}
}
function generateResult(answers) {
if(answers =="00000" || answers =="00001" || answers=="00010" || answers=="00100" || answers=="01000" || answers=="10000" || answers=="00011" || answers=="00110" || answers=="01100" || answers=="11000" || answers=="00101" || answers=="01010" || answers=="10100" || answers=="01001" || answers=="10010" || answers=="10001") return "ques2.html";
else if(answers=="11111" || answers=="11110" || answers=="11101" || answers=="11011" || answers=="10111" || answers=="01111" || answers=="11100" || answers=="11001" || answers=="10011" || answers=="00111" || answers=="11010" || answers=="10101" || answers=="01011" || answers=="10110" || answers=="01101" || answers=="01110") return "ques3.html";
else return "new.html";
}
function redirect(url) {
window.location = url;
}
你的「click handler」選項在哪裏(以及如何)clickClick(ans)被調用? 你試過重定向(generateResult(aString))嗎? 在optionClick中製作一個console.log(aString)... – Lemonade