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

时间:2018-03-06 19:21:09

标签: javascript arrays

我正在尝试循环查看包含我所有琐事问题的Div列表。我知道如何使用迭代器循环遍历所有这些,但我只想知道如何获取一个Div,运行一些代码,然后在用户单击提交后循环到数组中的下一个对象,然后运行相同的代码等我也隐藏了每个div,所以他们一次只能看到一个Div,直到他们完成上一个问题。 Java Script非常新,并且尽可能快地学习,任何提示都会非常感激

 <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>

这是我的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);
});

3 个答案:

答案 0 :(得分:2)

你必须记住最后一个位置。

所以你有一个变量,称之为lastGame之类的东西。这将是与correctAnswers等级相同的变量。从零开始。

每当您想要导航到下一个门时,请增加此值并执行gameArr[lastGame].show();

示例:

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

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

在超时时,您拨打nextGame()

答案 1 :(得分:0)

每当用户点击答案时,使全局迭代器增加其值,隐藏旧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];

然后提交表格后

$('.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();
      });
    }
});

答案 2 :(得分:0)

首先,每个表单不应该有多个类型提交按钮。将提交按钮移动到表单结束标记之前。

每个问题都不需要变量。使用你的jquery(暂时没有使用jquery我现在都做出反应)你应该能够选择这样的问题

var questions = $('form div')

然后你需要一个当前的问题变量。也许所有的div都有一个隐藏的类。选择当前问题时,从当前div中删除该隐藏类。

当用户提交表单时,如果不增加当前问题并显示下一个问题的div,则可以检查它们是否在问题数组的末尾。你可以隐藏上一个问题或让它保持可见(由你决定)。