简体   繁体   English

如何创建在不同页面上有不同问题的测验?

[英]How to create a quiz that has different questions on different pages?

I want to create a small quiz at the end of each of page.我想在每一页的末尾创建一个小测验。 I have a quiz written on JavaScript but I want different questions to appear in different pages.我有一个用 JavaScript 编写的测验,但我希望不同的问题出现在不同的页面中。 In a few words, I want the const myQuestions to change if i am on different pages.简而言之,如果我在不同的页面上,我希望 const myQuestions 改变。 How can I achieve this?我怎样才能做到这一点?

I already tried with if statements but I couldn't achieve what I want.我已经尝试过 if 语句,但我无法实现我想要的。 Any help would be greatly appreciated!任何帮助将不胜感激!

This is the JS code of the quiz这是测验的JS代码

(function() {

  const myQuestions = [
    {
      question: "question 1?",
      answers: {
        a: "one",
        b: "two",
        c: "three"
      },
      correctAnswer: "c"
    },
    {
      question: "question 2?",
      answers: {
        a: "one",
        b: "two",
        c: "three"
      },
      correctAnswer: "c"
    },
    {
      question: "question 3?",
      answers: {
        a: "one",
        b: "two",
        c: "three"
        d: "four"
      },
      correctAnswer: "d"
    }
  ];



  function buildQuiz() {
    // we'll need a place to store the HTML output
    const output = [];

    // for each question...
    myQuestions.forEach((currentQuestion, questionNumber) => {
      // we'll want to store the list of answer choices
      const answers = [];

      // and for each available answer...
      for (letter in currentQuestion.answers) {
        // ...add an HTML radio button
        answers.push(
          `<label>
             <input type="radio" name="question${questionNumber}" value="${letter}">
              ${letter} :
              ${currentQuestion.answers[letter]}
           </label>`
        );
      }

      // add this question and its answers to the output
      output.push(
        `<div class="slide">
           <div class="question"> ${currentQuestion.question} </div>
           <div class="answers"> ${answers.join("")} </div>
         </div>`
      );
    });

    // finally combine our output list into one string of HTML and put it on the page
    quizContainer.innerHTML = output.join("");
  }

  function showResults() {
    // gather answer containers from our quiz
    const answerContainers = quizContainer.querySelectorAll(".answers");

    // keep track of user's answers
    let numCorrect = 0;

    // for each question...
    myQuestions.forEach((currentQuestion, questionNumber) => {
      // find selected answer
      const answerContainer = answerContainers[questionNumber];
      const selector = `input[name=question${questionNumber}]:checked`;
      const userAnswer = (answerContainer.querySelector(selector) || {}).value;

      // if answer is correct
      if (userAnswer === currentQuestion.correctAnswer) {
        // add to the number of correct answers
        numCorrect++;

        // color the answers green
        answerContainers[questionNumber].style.color = "lightgreen";
      } else {
        // if answer is wrong or blank
        // color the answers red
        answerContainers[questionNumber].style.color = "red";
      }
    });

    // show number of correct answers out of total
    resultsContainer.innerHTML = `${numCorrect} out of ${myQuestions.length}`;
  }

  function showSlide(n) {
    slides[currentSlide].classList.remove("active-slide");
    slides[n].classList.add("active-slide");
    currentSlide = n;

    if (currentSlide === 0) {
      previousButton.style.display = "none";
    } else {
      previousButton.style.display = "inline-block";
    }

    if (currentSlide === slides.length - 1) {
      nextButton.style.display = "none";
      submitButton.style.display = "inline-block";
    } else {
      nextButton.style.display = "inline-block";
      submitButton.style.display = "none";
    }
  }

  function showNextSlide() {
    showSlide(currentSlide + 1);
  }

  function showPreviousSlide() {
    showSlide(currentSlide - 1);
  }

  const quizContainer = document.getElementById("quiz");
  const resultsContainer = document.getElementById("results");
  const submitButton = document.getElementById("submit");

  // display quiz right away
  buildQuiz();

  const previousButton = document.getElementById("previous");
  const nextButton = document.getElementById("next");
  const slides = document.querySelectorAll(".slide");
  let currentSlide = 0;

  showSlide(0);

  // on submit, show results
  submitButton.addEventListener("click", showResults);
  previousButton.addEventListener("click", showPreviousSlide);
  nextButton.addEventListener("click", showNextSlide);
})();

Mileage may vary, depends on what you want to do.里程可能会有所不同,这取决于您想要做什么。 With just javascript, you could pass windows.location.pathname to your function each time it's called and choose the appropriate object of questions related to the path you need.只需使用 javascript,您就可以在每次调用时将windows.location.pathname传递给您的函数,并选择与您需要的路径相关的适当问题对象。

if (windows.location.pathname).includes('page1') {
  let currentQuestion = myQuestions[0]
} 
//...rest of the logic

暂无
暂无

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

相关问题 我怎样才能在 axios 的反应中让我的每个测验问题及其在不同页面中的选择 - How can I have each of my my quiz questions and their choices in different pages in react by axios 如何在 MCQ 测验中突出显示不同问题的正确答案? - How to highlight correct answers for different questions in a MCQ quiz? 测验以启动基于不同终端屏幕的页面 - Quiz to launch pages based on different Endscreens 如何创建具有三种不同答案模式的测验应用程序? - How do I create a quiz app with three different answer modes? 如何创建下一个和上一个按钮以使用数组存储作为对象的问题在测验中选择问题? - How can create next and previous button's to select questions in a quiz using an array to store the questions which are objects? 试图用可变问题进行测验,无法让按钮给出不同的真/假值 - Trying to make a quiz with variable questions, unable to get the buttons to give different true/false values 在线测验:通过选择不同的单选按钮,将答案和问题分为3个类别 - Online-Quiz: Dividing the answers and questions in to 3 Categories by choosing different Radio-Buttons 如何从一个模板创建多个不同语言的页面? - How to create multiple pages with different languages from one template? 如何在&#39;gatsby-node&#39;中使用不同的模板创建多个页面? - how to create multiple pages using different templates in 'gatsby-node'? 如何使用反应路由创建具有不同标题的多页 - how to create multi pages with different headers using react routes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM