简体   繁体   English

Javascript 在 html 上正确显示问题和答案并加载测验的下一个问题

[英]Javascript display questions and answers on html correctly and load next question of a quiz

I´m making a quiz with javascript and I have encountered a problem with few things.我正在用 javascript 做一个测验,但我遇到了一些问题。 The quiz is works so that the user clicks the checkbox that has the correct answer.测验有效,因此用户可以单击具有正确答案的复选框。

My HTML form looks like this, so the correct answer is checked here in html part:我的 HTML 表单看起来像这样,所以在 html 部分检查正确答案:

<div id="kysymys"> <p id="kymysys"> </p> </div>
    <div id="answerit"></div>
    <div id="vastausformi">
        <form id="vastaukset"> 
            <label for="a">A)</label>
            <input type="radio" classname="box" name="box" id="check1" value="a" onclick="checkGuess(this.value)" /><br>
    
            <label for="b">B)</label>
            <input type="radio" classname="box" name="box" id="check2" value="b" onclick="checkGuess(this.value)" /><br>
             
            <label for="c">C)</label>
            <input type="radio" classname="box" name="box" id="check3" value="c" onclick="checkGuess(this.value)" /><br>
             
            <label for="d">D)</label>
            <input type="radio" classname="box" name="box" id="check4" value="d" onclick="checkGuess(this.value)" />
        </form>
    </div>

So in my JS I´m trying to get the questions and the answers to be displayed on html.所以在我的 JS 中,我试图让问题和答案显示在 html 上。 My javascript looks like this:我的 javascript 看起来像这样:

const questions= {
  
  question: "Which city is the capital of Sweden?",
  vastaus:{
      a: 'Stockholm',
      b: 'Malmö',
      c: 'Göteborg',
      d: 'Helsingborg'
  },
  correctAnswer: 'a',

  
  question: "Which animal can be seen on the Porsche logo?",
  vastaus:{
      a:'Buffalo',
      b:'Horse',
      c:'Jaguar',
      d:'Deer'
  },
  correctAnswer:'b'
};



function showAnswers(){
  for (const vastaus in questions) {
  document.getElementById("answerit").innerHTML=`${vastaus}: ${questions.vastaus}`.toString();
}}
showAnswers();


function showQuestion(){
  
  for (const question in questions) {
    document.getElementById("kymysys").innerHTML = `${question}: ${questions[question]}`.toString();
    
}
}
  showQuestion();

For some reason, the part that gets displayed is only the "correctAnswer:'b'" part and nothing else.由于某种原因,显示的部分只是“correctAnswer:'b'”部分,没有其他内容。 I also have problem with "displaying only one question", so it always shows the last/latest question added to javascript.我也有“只显示一个问题”的问题,所以它总是显示添加到 javascript 的最后一个/最新的问题。

I have tried to add a compeletely new div-element to html that would hold the answers, but it doesn´t work, it only shows the 'correctAnswer: b'- part.我试图在 html 中添加一个全新的 div 元素来保存答案,但它不起作用,它只显示“correctAnswer: b”部分。 I have tried to use loops so that it would go through the questions one at a time so that it would only show the one that I want, but it didn´t work so I took it out, since I can´t get those things to display properly even without it.我曾尝试使用循环,这样它就可以一次回答一个问题,这样它只会显示我想要的问题,但它没有用,所以我把它去掉了,因为我无法得到那些东西即使没有它也能正常显示。

Also for the other problem, displaying only question at a time and going to the other question after clicking a button, I have tried loops to go through the questions, but with no luck.同样对于另一个问题,一次只显示问题并在单击按钮后转到另一个问题,我尝试循环遍历这些问题,但没有成功。 This second problem (displaying one question etc.) is bigger so I will probably just do other post with better description of it, but if anyone happens to know a way with only this, I would be thankfullness.第二个问题(显示一个问题等)更大,所以我可能会做其他更好描述的帖子,但如果有人碰巧知道只有这个的方法,我会很感激。

I´m sorry for my english, but hopefully you will understand what I mean.我很抱歉我的英语不好,但希望你能明白我的意思。 If anyone knows how fix this I would be greatful.如果有人知道如何解决这个问题,我将不胜感激。 I have little experience with coding so things I do might not be correct.我没有编码经验,所以我做的事情可能不正确。 If you need more describing I will try.如果您需要更多描述,我会尝试。

You can try the code below, it shows all the questions and answers as it's a demo for now, but you can improve it as you need, you are good to move forward!你可以试试下面的代码,它显示了所有的问题和答案,因为它现在是一个演示,但你可以根据需要改进它,你很高兴继续前进!

 const questions = [ { question: "Which city is the capital of Sweden?", vastaus: { a: "Stockholm", b: "Malmö", c: "Göteborg", d: "Helsingborg" }, correctAnswer: "a" }, { question: "Which animal can be seen on the Porsche logo?", vastaus: { a: "Buffalo", b: "Horse", c: "Jaguar", d: "Deer" }, correctAnswer: "b" } ]; let index = 0; function showAnswer(index) { if(index < questions.length && index >= 0){ document.getElementsByClassName("answerit")[0].innerHTML = questions[index].question; document.getElementsByTagName("label")[0].textContent = "A) " + questions[index].vastaus.a; document.getElementsByTagName("label")[1].textContent = "B) " + questions[index].vastaus.b; document.getElementsByTagName("label")[2].textContent = "C) " + questions[index].vastaus.c; document.getElementsByTagName("label")[3].textContent = "D) " + questions[index].vastaus.d; }else{ alert("The end;"); } } showAnswer(index). document.getElementById("next"),addEventListener("click"; function () { showAnswer(++index); }). document.getElementById("prev"),addEventListener("click"; function () { showAnswer(--index); });
 <div id="kysymys"> <p id="kymysys"> </p> </div> <div class="answerit"></div> <div id="vastausformi"> <form id="vastaukset"> <label for="a">A)</label> <input type="radio" classname="box" name="box" id="check1" value="a" onclick="checkGuess(this.value)" /><br> <label for="b">B)</label> <input type="radio" classname="box" name="box" id="check2" value="b" onclick="checkGuess(this.value)" /><br> <label for="c">C)</label> <input type="radio" classname="box" name="box" id="check3" value="c" onclick="checkGuess(this.value)" /><br> <label for="d">D)</label> <input type="radio" classname="box" name="box" id="check4" value="d" onclick="checkGuess(this.value)" /> <br /> <input type="button" id="prev" value="Prev Question"> <input type="button" id="next" value="Next Question"> </form> </div>

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

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