通过jQuery提取JSON数据的问题

时间:2019-03-14 16:36:28

标签: javascript json

我正在尝试使用$ .getJSON方法提取一些json数据,但它对我不起作用。我对js很陌生,因此可以帮助您。在下面请找到json。

[
   {question: "What questions is it?",
      choices: ["answewr1", "answer2", "answer3"],
        correct:1},
   {question: "What questions is it?",
      choices: ["answewr1", "answer2", "answer3"],
        correct:3},
  {question: "What questions is it?",
      choices: ["answewr1", "answer2", "answer3"],
        correct:2},
  {question: "What questions is it?",
      choices: ["answewr1", "answer2", "answer3"],
        correct:1},
   {question: "What questions is it?",
      choices: ["answewr1", "answer2", "answer3"],
        correct:1}

  ];

在这里您可以找到脚本文件

$(document).ready(function() {
      var total_questions = 5;
      var questions = [];
      var questionNum = 0;
      var questionTotal = questions.length;
      var correctTotal = 0;
      $('#testQuestion').hide();
      $('#startQuizButton').click(function() {
        $('#message').hide();
        $('#startQuiz').hide();
        $('#testQuestion').show();
        $.getJSON('questions.json', function(data) {
          $.each(data, function(index, item) {
            $("#question").append(item);
          });
        });
        questionDisplay();
      });

      function questionDisplay() {
        $('#questionNum').text("Question " + (questionNum + 1) + " of " +
          total_questions);
        $('#question').text(questions[questionNum].question);
        $('#choices').empty();
        var choiceTotal = questions[questionNum].choices.length;
        for (var i = 0; i < choiceTotal; i++) { //displays the answer 
          choices
          $('#choices').append("<input type='radio' class='guess' name='guess' 
            value = " + i + " > " + questions[questionNum].choices[i] + " < br > ");
          }
        }
      })

0 个答案:

没有答案