javascript - 通过 jquery 提取 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 > ");
}
}
})
解决方案
推荐阅读
- google-cloud-platform - Dataflow 停止流式传输到 BigQuery 且没有错误
- django - Django 查询集分组
- javascript - 在屏幕中间滚动元素的方式
- angular - 我在角度 6 中遇到了父组件方法的问题
- javascript - Javascript 不返回 AJAX 数据(JSON、PHP、AJAX)
- angular - 在 Ionic 的 html 文件中声明后如何将值更改为变量?
- c# - 制作多对多数字匹配器的更有效方法
- java - Elasticsearch java客户端初始化失败
- security - 不是所有通过网络服务器进行的数据交换都容易被拦截吗?
- arrays - Angular 7/Typescript:创建队列/方法数组