简体   繁体   English

试图用可变问题进行测验,无法让按钮给出不同的真/假值

[英]Trying to make a quiz with variable questions, unable to get the buttons to give different true/false values

This is my first question here.这是我在这里的第一个问题。 I'm pretty new to this and I'm not sure what's wrong.我对此很陌生,我不确定出了什么问题。

I'm trying to get my code to give a different value depending on the question that's being asked.我试图让我的代码根据被问到的问题给出不同的值。 It keeps giving results as if every time I pressed the correct answer.就好像每次我按正确答案一样,它都会不断给出结果。

function nextQuestionA()
{
 if (numPA == 1)
 {
   numPA = 2;
   if(document.getElementsByName("choiceB"))
   {
     contador = contador + 1; 
     console.log(contador,numPA); 
   }
   else {null}
 }
 if (numPA == 2)
 {
   numPA = 3;
   if (document.getElementById("choiceC").onclick)
   {
     contador = contador + 1;
     console.log("contador");
   }
   else {null}
 }
}

Here's the HTML这是 HTML

<div id="animales">
  <h3>Trivia Animales</h3>
 <link rel= "stylesheet" href="style.css"> 
 <p4 id="a question number"></p4>
  <p5 id="questions animals"></p5>
  <button id="choiceA" value="a" onclick="nextQuestionA()"></button>
  <button id="choiceB" value="b" onclick="nextQuestionA()"></button>
  <button id="choiceC" value="c" onclick="nextQuestionA()"></button>
</div>

I also have the full code in Repl.it here: https://repl.it/@ClaviHaze/LightcoralCalculatingFields我在 Repl.it 中也有完整的代码: https://repl.it/@ClaviHaze/LightcoralCalculatingFields

Thank you in advance先感谢您

Revised Code jsfiddle: https://jsfiddle.net/zeamq4y9/修改后的代码jsfiddle: https://jsfiddle.net/zeamq4y9/

This is as far as I can take you unless you provide more information about what you want your code to do in terms of functionality.除非您提供有关您希望代码在功能方面做什么的更多信息,否则这是我所能带给您的。 Do you want to go to question #2 after the user selects an answer?在用户选择答案后,您想 go 对问题 #2 进行提问吗? Or do you want the user to stay on that same question?或者您是否希望用户停留在同一个问题上? I didn't see anywhere in the code where you are doing anything besides printing items to the console after a button is clicked.在单击按钮后,除了将项目打印到控制台之外,我没有在代码中看到您正在执行任何操作的任何地方。 I changed some of your JavaScript to JQuery to make the code more legible and I also fixed some of your syntax errors.我将您的一些 JavaScript 更改为 JQuery 以使代码更清晰,我还修复了您的一些语法错误。 Please review the jsfiddle and make improvements within your if and else statements.请查看 jsfiddle 并在您的ifelse语句中进行改进。 Some of your logic for your code is confusing such as there are two if statements back to back.您的一些代码逻辑令人困惑,例如有两个if语句背靠背。 You would need to accompany your first if with an else or and else if followed by an else .Do so, so that myself and others that are trying to help can understand the problem and better provide quality assistance with your problem.如果您的第一个ifelse或 and else if后跟else ,您将需要陪伴您。这样做,以便我自己和其他试图帮助的人能够理解问题并更好地为您的问题提供高质量的帮助。

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

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