[英]Need quiz application to display only one item at a time
I have created a quiz application, I want the application to display one question at a time when the user sits the quiz. 我创建了一个测验应用程序,我希望该应用程序在用户坐在测验中一次显示一个问题。 this worked fine until I populated the database with more than 5 questions now the quizzes displays all question could someone please tell me why this would be? 在我向数据库填充了5个以上的问题之前,这一直很好,现在测验显示所有问题,有人可以告诉我为什么会这样吗?
$(document).ready(function() {
var steps = $('form').find(".questions");
var count = steps.size();
steps.each(function(i) {
hider = i + count;
if (count == i + 1) {
var step = i + 1;
$("#next" + step).on('click', submit);
} else {
$("#question_" + hider).hide();
createNextButton(i);
}
});
function submit() {
$.ajax({
type: "POST",
url: "ajax.php",
data: $('form').serialize(),
success: function(msg) {
$("#quiz_form,#demo1").addClass("hide");
$('#result').show();
$('#result').append(msg);
}
});
}
function createNextButton(i) {
var step = i + 1;
var step1 = i + 2;
$('#next' + step).on('click', function() {
$("#question_" + step).hide();
$("#question_" + step1).show();
});
}
setTimeout(submit, 50000);
});
PHP: PHP:
$response = $db->prepare("select * from questions WHERE test_ID = '" . $_POST['test_ID'] . "'");
$response->execute();
echo "<form method='post' id='quiz_form'>";
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$count = 1;
while($result=$response->fetch(PDO::FETCH_ASSOC)) {
For starters, your hider
variable will always be +1 the size of your count. 对于初学者,您的hider
变量将始终为计数的+1。 If you have a .size()
of 5 questions, then (I would assume) the first iteration should hide, ie 如果您有5个问题的.size()
,则(我会假设) 应该隐藏第一次迭代,即
hider = i + count
hider = 0 + 5
hider = 5
$("#question_5").hide();
If, however, you are generating your questions on the PHP end w/ zero indexing, then your last question (your 5th), should have the id of question_4
. 但是,如果您在带有零索引的PHP端生成问题,那么最后一个问题(您的第5个问题)的ID 应该为question_4
。
As you can see, your hider
variable will never be less than 5, with each increment of 如您所见,您的hider
变量将永远不会小于5,且每次递增
steps.each(function(i) {
hider = i + count;
...
});
hider
will only increment from 5, up to 10 (ie #question_10
), which should not exist in your case. hider
只会从5增加到10(即#question_10
),在您的情况下不应该存在。
I would imagine you could make things simpler by just using 我想您可以通过使用以下方法使事情变得更简单
$("#question_" + i).hide();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.