簡體   English   中英

需要測驗的應用程序一次只顯示一項

[英]Need quiz application to display only one item at a time

我創建了一個測驗應用程序,我希望該應用程序在用戶坐在測驗中一次顯示一個問題。 在我向數據庫填充了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:

$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)) {

對於初學者,您的hider變量將始終為計數的+1。 如果您有5個問題的.size() ,則(我會假設) 應該隱藏第一次迭代,即

hider = i + count
hider = 0 + 5
hider = 5
$("#question_5").hide();

但是,如果您在帶有零索引的PHP端生成問題,那么最后一個問題(您的第5個問題)的ID 應該question_4

如您所見,您的hider變量將永遠不會小於5,且每次遞增

steps.each(function(i) {
   hider = i + count;
   ...
});

hider只會從5增加到10(即#question_10 ),在您的情況下不應該存在。

我想您可以通過使用以下方法使事情變得更簡單

$("#question_" + i).hide();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM