简体   繁体   English

Javascript 为我的测验创建上一个按钮

[英]Javascript create a previous button for my quiz

I'm creating a quiz like form and it's almost finished.我正在创建一个类似表格的测验,它几乎完成了。 There is just one thing that I can't seem to figure out.只有一件事我似乎无法弄清楚。 I've got a next, previous and result button but I can't seem to make the previous button work.我有一个下一个、上一个和结果按钮,但我似乎无法使上一个按钮工作。

    function PrevQuestion() {
        let oldAnswerButton = document.querySelectorAll('.filter_anwser');

        // Deletes old question when the next question is clicked
        for (let answerButton of oldAnswerButton) {
            answerButton.style.display = 'none';
        }

        let question = Quiz[questionNumber];

        // Displays answers of the questions
        for (let y = question.chosenAnswer.length; y > 0; y--) {
            let item = question.chosenAnswer[0];
            // Display answer buttons
            let btn = document.createElement('button');
            btn.value = item.id;
            btn.className = "filter_anwser";
            btn.textContent = item.name;
            button.appendChild(btn);
        }

        // Displays Question
        questionName.textContent = question.questionDescription;
        questionName.id = "questionID";

        // Deletes 1 to see the previous question
        questionNumber--;

    }

The code you see here is the function that belongs to the next button linked with an onclick.您在此处看到的代码是 function,它属于与 onclick 链接的下一个按钮。 Now i've tried to edit this so it does the exact opposite and show me the previous questions and answer buttons but it doesn't work.现在我尝试编辑它,让它完全相反,并向我显示以前的问题和答案按钮,但它不起作用。 The function should look a little like this but I just can't figure it out. function 应该看起来有点像这样,但我就是想不通。 Can anybody help me?有谁能够帮我?

Full code:完整代码:

<link rel="stylesheet" href="./style.css">
<div class="container">
    <div id="question"></div>
    <div id="answer"></div>
    <button onclick="NextQuestion()" id="nextbtn">Next</button>
    <button onclick="PrevQuestion()" id="prevbtn">Previous</button>
    <div id="finalLink"></div>
</div>
<script type="text/javascript">

    class QuizPart{
        constructor(questionDescription, chosenAnswer, prefix){
            this.questionDescription = questionDescription;
            this.chosenAnswer = chosenAnswer;
            this.prefix = prefix;
        }
    }
    
    class ChosenAnswer{
        constructor(id, name){
            this.id = id;
            this.name = name;
        }
    }

    let Quiz = [
        new QuizPart('Whats your size?', [
            new ChosenAnswer('6595', '41'),
            new ChosenAnswer('6598', '42'),
            new ChosenAnswer('6601', '43'),
        ], 'bd_shoe_size_ids='),

        new QuizPart('What color would you like?', [
            new ChosenAnswer('6053', 'Red'),
            new ChosenAnswer('6044', 'Blue'),
            new ChosenAnswer('6056', 'Yellow'),
            new ChosenAnswer('6048', 'Green'),
        ], 'color_ids='),

        new QuizPart('What brand would you like?', [
            new ChosenAnswer('5805', 'Adidas'),
            new ChosenAnswer('5866', 'Nike'),
            new ChosenAnswer('5875', 'Puma'),
        ], 'manufacturer_ids=')
    ]
    // console.log(Quiz);
    console.log(Quiz.length);

    let url = [];

    let questionNumber = 0;
    let button = document.getElementById('answer');
    let questionName = document.getElementById('question');
    let nextbtn = document.getElementById('nextbtn');
    let resultbtn = document.getElementById('FLink')

function NextQuestion() {
        let oldAnswerButton = document.querySelectorAll('.filter_anwser');

        // Deletes old question when the next question is clicked
        for (let answerButton of oldAnswerButton) {
            answerButton.style.display = 'none';
        }

        let question = Quiz[questionNumber];

        // Displays answers of the questions
        for (let y = 0; y < question.chosenAnswer.length; y++) {
            let item = question.chosenAnswer[y];
            // Display answer buttons
            let btn = document.createElement('button');
            btn.value = item.id;
            btn.className = "filter_anwser";
            btn.textContent = item.name;
            button.appendChild(btn);
        }
        // Check if your at the last question so the next button will stop being displayed.
        if(Quiz.length - 1 === questionNumber){
            nextbtn.style.display = 'none';
            resultbtn.style.display = 'grid';
        }

        // Displays Question
        questionName.textContent = question.questionDescription;
        questionName.id = "questionID";

        // adds 1 to question to see a different question
        questionNumber++;
}

    function PrevQuestion() {
        let oldAnswerButton = document.querySelectorAll('.filter_anwser');

        // Deletes old question when the next question is clicked
        for (let answerButton of oldAnswerButton) {
            answerButton.style.display = 'none';
        }

        let question = Quiz[questionNumber];

        // Displays answers of the questions
        for (let y = question.chosenAnswer.length; y > 0; y--) {
            let item = question.chosenAnswer[0];
            // Display answer buttons
            let btn = document.createElement('button');
            btn.value = item.id;
            btn.className = "filter_anwser";
            btn.textContent = item.name;
            button.appendChild(btn);
        }

        // Displays Question
        questionName.textContent = question.questionDescription;
        questionName.id = "questionID";

        // Deletes 1 to see the previous question
        questionNumber--;

    }

</script>

When you write a for loop, there are 3 parts within the brackets.当您编写一个for循环时,括号内有 3 个部分。 The first part declares a variable which is scoped to the loop.第一部分声明了一个作用域为循环的变量。 The second part is the condition for when the loop should finish.第二部分是循环何时结束的条件。 The third part is how the variable should change after each time round the loop.第三部分是在每次循环后变量应该如何变化。

You can see this working perfectly in NextQuestion :您可以在NextQuestion中看到它完美运行:

for (let y = 0; y < question.chosenAnswer.length; y++) {... }

The first part declares a variable called y and sets its value to zero.第一部分声明一个名为y的变量并将其值设置为零。 Each time round the loop, the third part runs, so y becomes 1 , then 2 , and so on.每次循环,第三部分都会运行,因此y变为1 ,然后变为2 ,依此类推。 Each time, the second part is checked to see if the loop should continue.每次,都会检查第二部分以查看循环是否应该继续。 If y is still smaller than the length of question.chosenAnswer then the loop will run again, but at some point y becomes equal to question.chosenAnswer.length and the loop stops.如果y仍然小于question.chosenAnswer的长度,则循环将再次运行,但在某个点y变得等于question.chosenAnswer.length并且循环停止。 The key point is that y isn't getting bigger automatically, it's getting bigger because that's what we've specified in the third part of the loop.关键是y不会自动变大,它会变大,因为这是我们在循环的第三部分中指定的。

Now let's have a look at your loop in PrevQuestion :现在让我们看看PrevQuestion中的循环:

for (let y = 0; y > question.chosenAnswer.length; y--) {... }

Again y starts at zero, but this time we are decreasing it's value each time (using y-- ) so it's value becomes -1 then -2 and so on. y再次从零开始,但这次我们每次都减少它的值(使用y-- ),所以它的值变为-1然后-2等等。 y keeps getting smaller so it never ends up larger than question.chosenAnswer.length , which is what would stop the loop. y不断变小,因此它永远不会大于question.chosenAnswer.length ,这将停止循环。 The consequence of this is that your loop will run infinitely, and the code after the loop will never execute.这样做的结果是您的循环将无限运行,并且循环之后的代码将永远不会执行。

It's hard to know for sure without seeing the full code, but most likely you want y++ same as in NextQuestion .如果没有看到完整的代码,很难确定,但很可能您希望y++NextQuestion中的相同。 An alternative would be to keep y-- but change the variable's initial variable and the end condition.另一种方法是保留y--但更改变量的初始变量和结束条件。 For example, this would be fine:例如,这会很好:

for (let y = question.chosenAnswer.length; y > 0; y--) {... }

Which is best in your case simply depends on which order you want y to take the values from 0 to question.chosenAnswer.length - if it should go from smallest to largest, or the other way round.在您的情况下哪个最好取决于您希望y将值从0question.chosenAnswer.length的顺序 - 如果它应该 go 从最小到最大,或者相反。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM