简体   繁体   English

如何从div数组中获取下一个对象?

[英]How can i grab the next object from an array of divs?

I am trying to cycle through a list of Div's that have all of my trivia questions inside. 我正在尝试浏览所有包含所有琐事问题的Div列表。 I know how to cycle through all of them with an iterator but i just want to know how to grab one Div, run some code, then cycle to the next object in the array after the user has clicked submit, then run the same code etc. I also have each div hidden so they can only see one Div at a time until they are done with the previous question. 我知道如何使用迭代器循环遍历所有对象,但是我只想知道如何获取一个Div,运行一些代码,然后在用户单击Submit之后循环到数组中的下一个对象,然后运行相同的代码,等等。 。我还隐藏了每个div,以便他们一次只能看到一个Div,直到完成上一个问题为止。 Very new to Java Script and trying to learn as fast as i can, any tips would greatly be appreciated Java脚本非常新并且尝试了尽可能快地学习,任何技巧都将不胜感激

 <div class="row">
            <div class="col-12 questions">
                <form name="quiz" onsubmit="return false">
                    <div class="q1">

                        <h1>The 'Mountain' is the nickname for which character?</h1>
                        <input type="radio" name="q" value="wrong" id="q1a">a. &nbsp; Gerold Clegane
                        <br>
                        <input type="radio" name="q" value="wrong" id="q1b">b. &nbsp; Sandor clegane
                        <br>
                        <input type="radio" name="q" value="wrong" id="q1c">c. &nbsp; Oberyn Martell
                        <br>
                        <input type="radio" name="q" value="correct" id="q1d">d. &nbsp; Gregor Clegane
                        <br>
                        <button class="submit"  type="submit">Submit</button>
                    </div>

                    <div class="q2">
                        <h2>Who is the youngest child of Lord Tywin Lannister?</h2>
                        <input type="radio" name="q" value="correct" id="q2a">a. &nbsp; Tyrion Lannister
                        <br>
                        <input type="radio" name="q" value="wrong" id="q2b">b. &nbsp; Jaime Lannister
                        <br>
                        <input type="radio" name="q" value="wrong" id="q2c">c. &nbsp; Cersei Lannister
                        <br>
                        <input type="radio" name="q" value="wrong" id="q2d">d. &nbsp; Jon Snow
                        <br>
                        <button class="submit"  type="submit">Submit</button>
                    </div>
                    <div class="q3">

                        <h3>Who is the King of the North?</h3>
                        <input type="radio" name="q" value="wrong" id="q3a">a. &nbsp; Bran Stark
                        <br>
                        <input type="radio" name="q" value="correct" id="q3b">b. &nbsp; Jon Snow
                        <br>
                        <input type="radio" name="q" value="wrong" id="q3c">c. &nbsp; Tommen Baratheon
                        <br>
                        <input type="radio" name="q" value="wrong" id="q3d">d. &nbsp; LittleFinger
                        <br>
                        <button class="submit"  type="submit">Submit</button>
                    </div>
                    <div class="q4">

                        <h4>Who is the head of house Stark?</h4>
                        <input type="radio" name="q" value="wrong" id="q4a">a. &nbsp; Tyrion Lannister
                        <br>
                        <input type="radio" name="q" value="wrong" id="q4b">b. &nbsp; Jon Snow
                        <br>
                        <input type="radio" name="q" value="wrong" id="q4c">c. &nbsp; Bran Stark
                        <br>
                        <input type="radio" name="q" value="correct" id="q4d">d. &nbsp; Ned Stark
                        <br>
                        <button class="submit"  type="submit">Submit</button>
                    </div>
                    <div class="q5">

                        <h5>Which persons are the 'Night's Watch' trying to stop by using a giant wall of ice?</h5>
                        <input type="radio" name="q" value="correct" id="q5a">a. &nbsp; White Walkers
                        <br>
                        <input type="radio" name="q" value="wrong" id="q5b">b. &nbsp; Wildings
                        <br>
                        <input type="radio" name="q" value="wrong" id="q5c">c. &nbsp; Mother of Dragons
                        <br>
                        <input type="radio" name="q" value="wrong" id="q5d">d. &nbsp; Night walkers
                        <br>
                        <button class="submit"  type="submit">Submit</button>
                    </div>
                </form>

Here is my Javascript 这是我的Javascript

$(document).ready(function () {
console.log("ready!");

var question1 = $('.q1');
var question2 = $('.q2');
var question3 = $('.q3');
var question4 = $('.q4');
var question5 = $('.q5');

var correctAnswers = 0;
var wrongAnswers = 0;
var gameArr = [question1, question2, question3, question4, question5];

function nextGame() {
    for (i=0;i<gameArr.length;i++){
        //i dont want it to grab every object all at the same time
    }



}
nextGame();
console.log(nextGame());

//My startGame button
$('.b1').on("click", function () {
    console.log(gameArr[0]);
    gameArr[0].show();
});

//Setting up audio for the start screen, make it loop so it never stops running
var audio1 = new Audio("assets/music/startMusic.mp3");

audio1.addEventListener('ended', function () {
    this.currentTime = 0;
    this.play();
}, false);
audio1.play();

//code to make my quiz responsive
function game() {

    var answer = $("input[name='q']:checked").val();
    if (answer === "correct") {
        alert("Correct!");
        correctAnswers = correctAnswers + 1

    } else {
        wrongAnswers = wrongAnswers + 1
        alert("Wrong!");

    }
}

//timer
function countdown() {
    seconds = 60;
    $('#timer').html('<h6>Time Remaining: ' + seconds + '</h6>');

    time = setInterval(showCountdown, 1000);
}

function showCountdown() {
    seconds--;
    $('#timer').html('<h3>Time Remaining: ' + seconds + '</h3>');
    if (seconds < 1) {
        clearInterval(time);


    }
}

//submit your answer
$('.submit').on("click", function () {
    //nextGame();
    game();

    console.log(gameArr);
});

You have to remember the last position. 您必须记住最后一个职位。

So you have a variable, call it lastGame or something. 因此,您有一个变量,将其称为lastGame之类。 This will be a variable at the same level as correctAnswers etc. Start with zero. 这将是与correctAnswers等级别相同的correctAnswers 。从零开始。

Whenever you want to navigate to the next gate, increment this and do a gameArr[lastGame].show(); 每当您想导航到下一个门时,请增加它并执行gameArr[lastGame].show(); .

Example: 例:

var correctAnswers = 0;
var wrongAnswers = 0;
var gameArr = [question1, question2, question3, question4, question5];
var lastGame = 0;

function nextGame() {
    gameArr[lastGame].show();
    lastGame++;
}

And on timeout you call nextGame() 然后在超时时调用nextGame()

make a global iterator increase its value every time the user hits the answer , hide the old div display the new Div something like this 每当用户点击答案时,使全局迭代器增加其值,隐藏旧的div显示新的Div,如下所示

var question1 = $('.q1');
var question2 = $('.q2');
var question3 = $('.q3');
var question4 = $('.q4');
var question5 = $('.q5');

var correctAnswers = 0;
var wrongAnswers = 0;
var currentIndex = 0;
var gameArr = [question1, question2, question3, question4, question5];

then after the form is submitted 然后提交表格后

$('.submit').on("click", function () {
    //nextGame();
    game();

    if(currentIndex  < (gameArr.length -1 ))
    {
      // I am using the hide callback which is fired by jQUery after the object is completely hidden
      gameArr[currentIndex].hide(function(){

         currentIndex++;
         gameArr[currentIndex].show();
      });
    }
});

First off, you should never have more than one button of type submit per form. 首先,每个表单的提交按钮数量不应超过一个。 Move the submit button to just before the form's end tag. 将提交按钮移到表单的结束标记之前。

You don't need variables for every question. 您不需要每个问题都包含变量。 With your jquery (haven't used jquery in awhile I'm all react now) you should be able to select your questions like this 使用您的jquery(我已经有一段时间没有使用jquery了,我现在已经做出反应了),您应该可以选择这样的问题

var questions = $('form div')

Then you will need a current question variable. 然后,您将需要一个当前的问题变量。 Maybe have all the divs with a hidden class. 也许所有的div都有一个隐藏的类。 When the current question is selected remove that hidden class from the current div. 选择当前问题后,从当前div中删除该隐藏的类。

when the user submits the form you can check if they are at the end of the array of questions if not increment the current question and show the div for the next question. 当用户提交表单时,如果不增加当前问题并显示下一个问题的div,则可以检查它们是否位于问题数组的末尾。 You could hide the previous question or keep it visible (up to you). 您可以隐藏上一个问题或使其可见(由您自己决定)。

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

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