分支逻辑测验——我错在哪里?[JavaScript]
我试图为一个“简单”的jQuery测试开发逻辑。问题是,问题路径是动态的,并生成一个树结构 例如,第一个问题是关于宠物的:狗、猫还是鸟?如果你选择狗,它会问什么品种的狗。如果你选择猫,它会问什么品种的猫。等等,然后再深入研究(“这种特殊品种的狗是什么颜色的?”) 我遇到的问题是第三组问题。我不能让颜色问题对任何一只狗起作用。事实上,click处理程序甚至不会启动。谁能告诉我我在哪里弄脏了这个 先谢谢你分支逻辑测验——我错在哪里?[JavaScript],javascript,jquery,html,css,arrays,Javascript,Jquery,Html,Css,Arrays,我试图为一个“简单”的jQuery测试开发逻辑。问题是,问题路径是动态的,并生成一个树结构 例如,第一个问题是关于宠物的:狗、猫还是鸟?如果你选择狗,它会问什么品种的狗。如果你选择猫,它会问什么品种的猫。等等,然后再深入研究(“这种特殊品种的狗是什么颜色的?”) 我遇到的问题是第三组问题。我不能让颜色问题对任何一只狗起作用。事实上,click处理程序甚至不会启动。谁能告诉我我在哪里弄脏了这个 先谢谢你 var questions = [{ "text": "What's your fav
var questions = [{
"text": "What's your favorite pet?",
"choices": [
{
"label": "Dog",
"path": 1
},
{
"label": "Cat",
"path": 2
},
{
"label": "Bird",
"path": 3
},
]
}, {
"text": "What breed of dog?", //1 a
"choices": [
{
"label": "Golden Retriever",
"path": 4
},
{
"label": "Labrador",
"path": 5
},
{
"label": "Poodle",
"path": 6
},
]
}, {
"text": "What breed of cat?", //1 b
"choices": [
{
"label": "Siamese",
"path": 4
},
{
"label": "Persian",
"path": 4
},
{
"label": "Tortie",
"path": 4
},
]
}, {
"text": "What breed of bird?", //1 c
"choices": [
{
"label": "Bluebird",
"path": 4
},
{
"label": "Robin",
"path": 4
},
{
"label": "Parrot",
"path": 4
},
]
}, {
"text": "What color Golden Retriever?", //1 a i
"choices": [
{
"label": "Golden",
"path": 4
},
{
"label": "Sandy",
"path": 4
},
{
"label": "Blonde",
"path": 4
},
]
}, {
"text": "What color Labrador?", //1 b i
"choices": [
{
"label": "Black",
"path": 4
},
{
"label": "Chocolate",
"path": 4
},
{
"label": "Tan",
"path": 4
},
]
}, {
"text": "What color Poodle?", //1 c i
"choices": [
{
"label": "Ugly",
"path": 4
},
{
"label": "Uglier",
"path": 4
},
{
"label": "Ugliest",
"path": 4
},
]
}];
var currentQuestion = 0,
quizComplete = false;
$(document).ready(function() {
updateQuestion();
if (!quizComplete) {
$('.choice').on('click', function() {
value = $(this).attr('value');
currentQuestion = value;
if (currentQuestion < questions.length) {
updateQuestion();
} else {
quizComplete = true;
}
});
}
});
function updateQuestion() {
var question = questions[currentQuestion].text;
var questionText = $(document).find(".container > .question");
var choiceList = $(document).find(".container > .choices");
var numChoices = questions[currentQuestion].choices.length;
// Set question text
$(questionText).text(question);
// Clear current choices and update with new ones
$(".choice").remove();
var choice, path;
for (i = 0; i < numChoices; i++) {
choice = questions[currentQuestion].choices[i].label;
path = questions[currentQuestion].choices[i].path;
$('<div class="choice" value=' + path + '>' + choice + '</div>').appendTo(choiceList);
}
}
var问题=[{
“文本”:“你最喜欢的宠物是什么?”,
“选择”:[
{
“标签”:“狗”,
“路径”:1
},
{
“标签”:“猫”,
“路径”:2
},
{
“标签”:“鸟”,
“路径”:3
},
]
}, {
“文本”:“什么品种的狗?”,//1 a
“选择”:[
{
“标签”:“黄金猎犬”,
“路径”:4
},
{
“标签”:“拉布拉多”,
“路径”:5
},
{
“标签”:“贵宾犬”,
“路径”:6
},
]
}, {
“文本”:“什么品种的猫?”,//1b
“选择”:[
{
“标签”:“暹罗”,
“路径”:4
},
{
“标签”:“波斯语”,
“路径”:4
},
{
“标签”:“玉米饼”,
“路径”:4
},
]
}, {
“文本”:“什么品种的鸟?”,//1c
“选择”:[
{
“标签”:“蓝鸟”,
“路径”:4
},
{
“标签”:“罗宾”,
“路径”:4
},
{
“标签”:“鹦鹉”,
“路径”:4
},
]
}, {
“文本”:“什么颜色的金色猎犬?”,//1 a i
“选择”:[
{
“标签”:“金色”,
“路径”:4
},
{
“标签”:“桑迪”,
“路径”:4
},
{
“标签”:“金发女郎”,
“路径”:4
},
]
}, {
“文本”:“什么颜色的拉布拉多?”,//1 b i
“选择”:[
{
“标签”:“黑色”,
“路径”:4
},
{
“标签”:“巧克力”,
“路径”:4
},
{
“标签”:“棕色”,
“路径”:4
},
]
}, {
“文本”:“什么颜色的狮子狗?”,//1 c i
“选择”:[
{
“标签”:“丑陋”,
“路径”:4
},
{
“标签”:“更丑”,
“路径”:4
},
{
“标签”:“最丑”,
“路径”:4
},
]
}];
var currentQuestion=0,
quizComplete=false;
$(文档).ready(函数(){
updateQuestion();
如果(!quizComplete){
$('.choice')。在('click',function()上{
value=$(this.attr('value');
当前问题=价值;
如果(当前问题<问题长度){
updateQuestion();
}否则{
quizComplete=true;
}
});
}
});
函数updateQuestion(){
变量问题=问题[currentQuestion]。文本;
var questionText=$(document.find(“.container>.question”);
var choiceList=$(document.find(“.container>.choices”);
var numChoices=questions[currentQuestion].choices.length;
//设置问题文本
$(问题文本)。文本(问题);
//清除当前选项并使用新选项更新
$(“.choice”).remove();
var选择、路径;
对于(i=0;i
每次更新问题时都必须添加单击事件。这里更新了代码
var问题=[{
“文本”:“你最喜欢的宠物是什么?”,
“选择”:[
{
“标签”:“狗”,
“路径”:1
},
{
“标签”:“猫”,
“路径”:2
},
{
“标签”:“鸟”,
“路径”:3
},
]
}, {
“文本”:“什么品种的狗?”,//1 a
“选择”:[
{
“标签”:“黄金猎犬”,
“路径”:4
},
{
“标签”:“拉布拉多”,
“路径”:5
},
{
“标签”:“贵宾犬”,
“路径”:6
},
]
}, {
“文本”:“什么品种的猫?”,//1b
“选择”:[
{
“标签”:“暹罗”,
“路径”:4
},
{
“标签”:“波斯语”,
“路径”:4
},
{
“标签”:“玉米饼”,
“路径”:4
},
]
}, {
“文本”:“什么品种的鸟?”,//1c
“选择”:[
{
“标签”:“蓝鸟”,
“路径”:4
},
{
“标签”:“罗宾”,
“路径”:4
},
{
“标签”:“鹦鹉”,
“路径”:4
},
]
}, {
“文本”:“什么颜色的金色猎犬?”,//1 a i
“选择”:[
{
“标签”:“金色”,
“路径”:4
},
{
“标签”:“桑迪”,
“路径”:4
},
{
“标签”:“金发女郎”,
“路径”:4
},
]
}, {
“文本”:“什么颜色的拉布拉多?”,//1 b i
“选择”:[
{
“标签”:“黑色”,
“路径”:4
},
{
“标签”:“巧克力”,
“路径”:4
},
{
“标签”:“棕色”,
“路径”:4
},
]
}, {
“文本”:“什么颜色的狮子狗?”,//1 c i
“选择”:[
{
“标签”:“丑陋”,
“路径”:4
},
{
“标签”:“更丑”,
“路径”:4
},
{
“标签”:“最丑”,
“路径”:4
},
]
}];
var currentQuestion=0,
quizComplete=false;
$(文档).ready(函数(){
updateQuestion();
});
//分离下一个问题
函数nextQuestion(){
如果(!quizComplete){
$('.choice')。在('click',function()上{
value=$(this.attr('value');
当前问题=价值;
如果(当前问题<问题长度){
updateQuestion();
}否则{
quizComplete=true;
}
});
}
}
函数updateQuestion(){
变量问题=问题[currentQuestion]。文本;
var questionText=$(document.find(“.container>.question”);
var choiceList=$(document.find(“.container>.choices”);
var numChoices=问题[curr]