简体   繁体   中英

Onclick to a function doesn't work

im creating an online quiz with buttons, my problem is that the onclick button isn't really doing what i hoped it would, im a beginner in javascript so i was hoping people can give me solutions or alternative suggestions to make it work;

here is my code

<html>
<head>
<title> </title>
</head>
<body>

<div id="qholder"> </div>
<button name="choices" onclick="CheckAnswer('A')" > <p id="choice1"> </p> </button>

<button name="choices" onclick="CheckAnswer('B')" > <p id="choice2"> </p> </button>

<button name="choices" onclick="CheckAnswer('C')" > <p id="choice3"> </p> </button>

<button name="choices" onclick="CheckAnswer('D')" > <p id="choice4"> </p> </button>

<script>

var qpos = 0;
var correctans=0;
var answer=0;


var Quiz = [
    ["What team was the first TI Champion?", "Invictus Gaming", "Team Liquid", "Natus Vincere", "Orange E-Sports", 'C'],
    ["Who was the captain of the First TI Champion Team?", "Puppey", "Artstyle", "Kuroky", "xiao8", 'B'],
    ["Where does Natus Vincere Operate?", "USA", "Moscow", "Philippines", "Ukraine", 'D'],
    ["Who played Midlane for Natus Vincere?", "Miracle", "Suma1l", "Dendi", "Maybe", 'C'],
    ["How many TI grandfinals did Team Natus Vincere played in?", "3", "2", "1", "4", '1'],
    ["Who replaced LightofHeaven after leaving Natus Vincere?", "General", "Sonneiko", "rodger", "Funn1k", 'D'],
    ["Who defeated Na'Vi in the TI3 Grand Finals?", "Team Liquid", "Cloud 8", "Evil Geniuses", "Team Alliance", 'D'],
    ["Who is the current captain of Team Na'Vi?", "Pajkatt", "Cr1t", "Sonneiko", "Fly", 'C'],
    ["Who is the owner of Na'Vi?", "Gaben", "CyborgMatt", "ODpixel", "zer0gravity", 'D'],
    ["When was Team Natus Vincere Founded?", "July 1996", "December 2009", "November 2012", "March 2017", 'B']
];

function startquiz(){   
    getQuestions(); 
};  

function getQuestions() {

        document.getElementById("qholder").innerHTML = Quiz[qpos][0];
        document.getElementById("choice1").innerHTML = Quiz[qpos][1];
        document.getElementById("choice2").innerHTML = Quiz[qpos][2];
        document.getElementById("choice3").innerHTML = Quiz[qpos][3];
        document.getElementById("choice4").innerHTML = Quiz[qpos][4];

        };  


function CheckAnswer (answer){
    if(Quiz[qpos][5] == answer) {
      correctans + 1; 
    };
  getnextQuestion();    
};


function getnextQuestion() {
    qpos + 1;
    getQuestions();
};

startquiz();

</script>
</body>
</html>

pls help, i've been stuck for hours trying to figure it out, i used a multi dimensional array for my question, choices and answers and would like the button onclick to proceed to the next question while it checks if the clicked answer is right

qpos + 1

至少需要为qpos++qpos = qpos + 1

function CheckAnswer (answer){
    if(Quiz[qpos][5] == answer) {
        correctans++; 
    };
    getnextQuestion();  
};


function getnextQuestion() {
    qpos++;
    getQuestions();
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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