Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从阵列数据库自动化阵列对象显示_Javascript_Jquery_Html - Fatal编程技术网

Javascript 从阵列数据库自动化阵列对象显示

Javascript 从阵列数据库自动化阵列对象显示,javascript,jquery,html,Javascript,Jquery,Html,我有一个包含10个对象的数组。每个包含一个问题字符串、一个正确答案字符串和一个包含4个答案字符串的对象: const DB = [ { question: "some question", answers: ["a", "variety", "of", "choices"], correctAnswer: "variety" }, ... 我有一个功能,通过单选按钮输入捕获用户的答案,并将其保存在变量中: function feedbackPage(){ $('.js-quiz

我有一个包含10个对象的数组。每个包含一个问题字符串、一个正确答案字符串和一个包含4个答案字符串的对象:

const DB = [
{
  question: "some question",
  answers: ["a", "variety", "of", "choices"],
  correctAnswer: "variety"
}, ...
我有一个功能,通过单选按钮输入捕获用户的答案,并将其保存在变量中:

function feedbackPage(){
  $('.js-quizform-questions').on('click', '.js-button-next', function(event){
    event.preventDefault()
    let yourAnswer = getFeedback()
    $('.js-feedback-page').show().html(evalCorrectAnswer(DB))
  })
}
您可以看到evalCorrectAnswer将访问上述数据库进行验证。目前,我只能显示和验证硬编码的第一个答案,它是动态生成的,如下所示:

function generateQuestionElement(item) {
  return `
    <h2>${item[0].question}</h2>
    <form id='form'>
        <fieldset>
            <div class='css-answers'>
                <input id='answer1' type='radio' name='answer' required>
                <label for='answer1'>${item[0].answers[0]}</label>
            </div>
            <div class='css-answers'>
                <input id='answer2' type='radio' name='answer' required>
                <label for='answer2'>${item[0].answers[1]}</label>
我需要一种方法来自动选择第一个问题,包括它的答案,并通过generateQuestionItem显示它。然后,我需要将该问题及其答案从池中取出,并在单击评估页面上的“下一步”按钮后显示下一个问题。我很难用面向对象的方式实现这个特性


构造一个变量

let questionNumber = 0
然后更新renderQuestion内的以下行:

稍后创建一个函数,在每个适当的点更新questionNumber

<label for='answer1'>${DB[questionNumber].answers[0]}</label>