Jquery测验只显示第一页和最后一页
jquery quiz shows only first and last pages
我正在创建一个测验。有4个问题,每个问题有4个答案。问题是,在第一个问题加载后,您选择一个答案并按下一个按钮,然后加载最后一个问题而不是第二个问题。我如何停止执行?或者代码有什么问题?
你能帮我吗?
代码如下:
q1.toDOM();
clickAnswer();
$('.options').on('click', function() {
if (q1.check($(this).text()) === true) { // q1 check method's actual parameter (the answer which was selected by user), $(this).text()= answer
$('.answer').append('<h2>Your answer is correct!<br>Rolling Stones was established in 1962.</h2>');
} else {
$('.answer').append('<h2>Your answer is incorrect!<br>Rolling Stones was established in 1962.</h2>');
}
$('.next-button').show();
});
$('.next-button').on('click', function() {
console.log('question2');
indicateStep();
var q2 = questions[1]; //create the second question with the 0'th element of the Array
q2.toDOM();
clickAnswer();
$('.options').on('click', function() {
if (q2.check($(this).text()) === true) {
$('.answer').append('<h2>Your answer is correct!<br>Jean-Paul "Bluey" is the singer of Incognito</h2>');
} else {
$('.answer').append('<h2>Your answer is incorrect!<br>Jean-Paul "Bluey" is the singer of Incognito</h2>');
}
$('.next-button').show();
});
event.preventDefault;
});
$('.next-button').on('click', function() {
console.log('question3');
indicateStep();
var q3 = questions[2]; //create the second question with the 0'th element of the Array
q3.toDOM();
clickAnswer();
$('.options').on('click', function() {
if (q3.check($(this).text()) === true) {
$('.answer').append('<h2>Your answer is correct!<br>Michael Jackson sold 42.4 million albűlbums from Thriller</h2>');
} else {
$('.answer').append('<h2>Your answer is incorrect!<br>Michael Jackson sold 42.4 million albűlbums from Thriller</h2>');
}
$('.next-button').show();
});
});
您面临的错误是在这一部分。
$('.next-button').on('click', function() {
console.log('question2');
indicateStep();
var q2 = questions[1]; //create the second question with the 0'th element of the Array
q2.toDOM();
clickAnswer();
$('.options').on('click', function() {
if (q2.check($(this).text()) === true) {
$('.answer').append('<h2>Your answer is correct!<br>Jean-Paul "Bluey" is the singer of Incognito</h2>');
} else {
$('.answer').append('<h2>Your answer is incorrect!<br>Jean-Paul "Bluey" is the singer of Incognito</h2>');
}
$('.next-button').show();
});
event.preventDefault;
});
$('.next-button').on('click', function() {
console.log('question3');
indicateStep();
var q3 = questions[2]; //create the second question with the 0'th element of the Array
q3.toDOM();
clickAnswer();
$('.options').on('click', function() {
if (q3.check($(this).text()) === true) {
$('.answer').append('<h2>Your answer is correct!<br>Michael Jackson sold 42.4 million albűlbums from Thriller</h2>');
} else {
$('.answer').append('<h2>Your answer is incorrect!<br>Michael Jackson sold 42.4 million albűlbums from Thriller</h2>');
}
$('.next-button').show();
});
});
这个元素有2个事件处理程序。它总是一直向下移动到最后一个事件。我认为这个事件会为用户加载第四个问题。下面是示例
event triggered...
if answer correct
load second question
if answer correct
load third question
if answer corrent
load fourth question
您没有任何条件要检查,哪个问题已提交,哪个问题下一个加载。您应该试试这段代码,在下面的代码中,我将使用类检查条件,您也可以将条件更改为其他变量。查看用户当前正在处理哪个问题。
$('.next-button').on('click', function() {
if($(this).attr('class') == 'first-question') { // add condition
console.log('question2');
indicateStep();
var q2 = questions[1]; //create the second question with the 0'th element of the Array
q2.toDOM();
clickAnswer();
$('.options').on('click', function() {
if (q2.check($(this).text()) === true) {
$('.answer').append('<h2>Your answer is correct!<br>Jean-Paul "Bluey" is the singer of Incognito</h2>');
} else {
$('.answer').append('<h2>Your answer is incorrect!<br>Jean-Paul "Bluey" is the singer of Incognito</h2>');
}
$('.next-button').show();
});
event.preventDefault;
} else if ($(this).attr('class') == 'second-question') { // another condition
// code for second question...
}
});
…等等。
其次,只是一个建议,而不是一个答案。如果你添加
,这将是一个很好的用户体验不是正确答案||错误答案
长时间的错误或长时间的胜利都不好!你应该尽量减少它们。当前答案,[name]是《Incognito》的演唱者||错误答案,[name]从未演唱过《Incognito》。
将不同的函数绑定到同一个事件上无法实现连续更新。在用户点击.next-button
的那一刻,你应该知道已经通过了哪一步,下一步要显示哪一步(基于某种条件)。
我建议有一些全局变量,如window.current_question_id
,这将表明当前位置(这里的其他可能的选择是css
类和data
属性)。点击.next-button
将检查问题编号,并显示下一个问题(如果他们中的任何一个离开)。
看一下我发布的js代码,你会对应该发生的事情有一个基本的了解。
我也会把正确的答案封装在你的问题对象中(当你要求q3.check()
时,你已经做了一些)。这将使您不必重复代码(因为q2
和q3
显示的函数看起来非常相似)。
# we can get rid of three functions for click event
# lets make the signle function responsible for that instead
$('.next-button').on('click', function() {
# we should know which step we're at.
# I assume you have window.current_question_id set up
# and you handle the case of no more questions left by yourself here
var next_question = questions[window.current_question_id + 1];
next_question.toDOM();
clickAnswer();
$('.options').on('click', function() {
# lets assume answer is incorrect by default. one less condition!
var correctness = 'incorrect';
if (next_question.check($(this).text()) === true) {
correctness = 'correct';
}
var appending_text = '<h2> Your answer is ' + correctness + '!<br>';
# I noticed you show pretty the same string here regardless of correctness.
# why not to incapsulate it in your `q` objects so they just return that string?
appending_text = appending_text + next_question.correct_answer();
$('.answer').append(appending_text);
$('.next-button').show();
});
});
相关文章:
- 如何获取不属于我项目的上一页的URL
- 键入最后一位数字后自动提交
- 如何在Facebook上的iframe应用程序中使后退按钮返回到上一页
- 使用Booklet Jquery插件,我如何验证最后一页是否显示
- 如何在一页网站中使用PHP重定向到一个页面
- Javascript并不是显示在每一页上
- jQuery分页下一页和上一页按钮在点击零或超过最后一页后失败
- 如何为AngularJS制作智能表中第一页和最后一页的跳过按钮
- 从mysql数据库中删除项目,然后返回最后一页
- 区分剑道网格上一页/下一页点击与转到第一页/最后一页点击事件
- Listjs为筛选后的最后一页生成按钮
- 使用滚动重新加载 Jqgrid 时出现问题 - 重复的行数据并缺少最后一页
- 如何在多页面中制作一个 JavaScript 表单向导,其中包含下一页,并取消并返回并提交最后一页
- 数据表在页面加载时转到最后一页
- 使用pdfmake将文本固定到pdf的最后一页底部
- 如何在Javascript中加载KendoGrid的最后一页
- 如何添加'Done'按钮,位于多页导览的最后一步
- 更改最后一页URL
- Jquery测验只显示第一页和最后一页
- jQuery DataTable - init后通过JS跳转到最后一页