Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.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和html创建测验,但我的javascript测验内容不会显示在html中_Javascript_Html_Arrays - Fatal编程技术网

使用javascript和html创建测验,但我的javascript测验内容不会显示在html中

使用javascript和html创建测验,但我的javascript测验内容不会显示在html中,javascript,html,arrays,Javascript,Html,Arrays,我目前正在使用数组创建一个有10个问题的定时测验 我设法用JS在HTML中创建了一个计时器,但不知何故,我的测验(JS文件中的数组)没有显示在HTML中 我仔细检查了我的代码中是否没有bug,声明了所有变量,并确保在html代码中正确链接了javascript文件 下面是javascript部分。。。 (我没有发布我的测验问题) var myQuestions=[ {~~~~~我的问题就在这里} ] 函数generateQuiz(问题、quizContainer、结果Container、subm

我目前正在使用数组创建一个有10个问题的定时测验

我设法用JS在HTML中创建了一个计时器,但不知何故,我的测验(JS文件中的数组)没有显示在HTML中

我仔细检查了我的代码中是否没有bug,声明了所有变量,并确保在html代码中正确链接了javascript文件

下面是javascript部分。。。 (我没有发布我的测验问题)

var myQuestions=[
{~~~~~我的问题就在这里}
]
函数generateQuiz(问题、quizContainer、结果Container、submitButton){
函数showQuestions(问题,quizContainer){
//存储答案
var输出=[];
var答案;
//通过使用for循环。

对于(var i=0;i您声明了内部函数,但从未调用过它们。您可以将它们取出并像这样编写它们

var myQuestions=[
]
函数showQuestions(问题,quizContainer){
//存储答案
var输出=[];
var答案;
//通过使用for循环。
对于(var i=0;i}
我看到您使用的是嵌套函数,起初没有调用“showQuestions”方法。此外,您正在尝试从onclick事件调用“showResults”,该事件在嵌套函数的外部范围内初始化。您可以按以下方法进行尝试:

var myQuestions=[{
问题:“Lorem ipsum dolor”是什么意思?”,
回答:[什么都没有,'什么'],
正确答案:“1”
}];
函数generateQuiz(问题、quizContainer、结果Container、submitButton){
函数showQuestions(问题,quizContainer){
//存储答案
var输出=[];
var答案;
//通过使用for循环。
对于(var i=0;i

剩余时间=
取得成果

控制台中记录了哪些错误(如
F12
,控制台选项卡)?如果您是从其他来源复制的,请发布链接,因为您的文章太长,因此太难阅读。您应该发布您的co
var myQuestions = [
    { ~~~~~ my questions go here }
]



function generateQuiz(questions, quizContainer, resultsContainer, submitButton){

    function showQuestions(questions, quizContainer){
        //storing the answers
        var output = [];
        var answers;

        // by using for loops.
        for(var i=0; i<questions.length; i++){

            answers = [];

                for(letter in questions[i].answers){

                    // radio buttion in html
                    answers.push(
                        '<label>'
                            + '<input type="radio" name="question'+i+'" value="'+letter+'">'
                            + letter + ': '
                            + questions[i].answers[letter]
                        + '</label>'
                    );
                }

            // add the Q&A to results
            output.push(
                '<div class="question">' + questions[i].question + '</div>'
                + '<div class="answers">' + answers.join('') + '</div>'
            );
        }

        // join to html 
        quizContainer.innerHTML = output.join('');
    }



    // --------- showing results 

    function showResults(questions, quizContainer, resultsContainer){

        // collect answer containers quiz
        var answerContainers = quizContainer.querySelectorAll('.answers');

        // count correct/wrong answers
        var userAnswer = '';
        var numCorrect = 0;

        for(var i=0; i<questions.length; i++){


            userAnswer = (answerContainers[i].querySelector('input[name=question'+i+']:checked')||{}).value;

            // if/else loop 
            // correct answer, answer will display in green and # of correct answer will added
            // wrong answer, answer will display in red
            if(userAnswer===questions[i].correctAnswer){

                numCorrect++;
                answerContainers[i].style.color = 'lightgreen';
            }
            else{
                answerContainers[i].style.color = 'red';
            }
        }

        // show number of correct answers out of total
        resultsContainer.innerHTML = numCorrect + ' out of ' + questions.length;
    }

    // --------- declare variables and display in html

    var quizContainer = document.getElementById('quiz');
    var resultsContainer = document.getElementById('results');
    var submitButton = document.getElementById('submit');


    generateQuiz(myQuestions, quizContainer, resultsContainer, submitButton);


    // --------- showing results on submit

    submitButton.onclick = function(){
        showResults(questions, quizContainer, resultsContainer);
    }
<form method="POST" name="javaInterviewQuiz">
        <div id="time"><h4>Time left = <span id="timer"></span></h4></div>

        <div id="quiz"></div>
        <button id="submit">Get Results</button>
        <div id="results"></div>

</form>