Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.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 试图使用jquery将html元素数组附加到页面上的div元素,但它';它不显示_Javascript_Jquery_Html_Arrays - Fatal编程技术网

Javascript 试图使用jquery将html元素数组附加到页面上的div元素,但它';它不显示

Javascript 试图使用jquery将html元素数组附加到页面上的div元素,但它';它不显示,javascript,jquery,html,arrays,Javascript,Jquery,Html,Arrays,我试图在页面上的div元素中添加一个html元素数组,但它没有显示出来。当我在div元素上执行console.log时,它会打印数组,确认该数组已添加到div元素,但它没有出现在页面上。下面是我正在尝试执行的操作的代码: html: 转换问题: jquery: const $examQuestions = $('#exam-question'), output = []; loadQuestions($examId, function(question){ for(let i in

我试图在页面上的div元素中添加一个html元素数组,但它没有显示出来。当我在div元素上执行console.log时,它会打印数组,确认该数组已添加到div元素,但它没有出现在页面上。下面是我正在尝试执行的操作的代码:

html:


转换问题:
jquery:

const $examQuestions = $('#exam-question'),
output = [];

loadQuestions($examId, function(question){
    for(let i in question) {
        $switchQuestion.append($('<option />').val(question[i].id).text(Number(i)+1));
        loadChoices(question[i].id, question[i].multiAnswer, function(choices) {
            output.push(
                    `<div class='question'> ${(Number(i)+1)}. ${question[i].problemDescription} </div>
                     <div class='choices'> ${choices.join('')} </div>`
            );
        });
    }
});

output.join('');
$examQuestions.innerHTML = output;

function loadChoices(id, isMulti, callback) {
    let choices = [];
    $.ajax({
        url: location.protocol + '//' + location.host + '/online-test-exam-maker' +'/choices/' + id,
        dataType: 'json',
        type: 'GET',
        success: function (data, status) {
            let elemType = isMulti ? 'checkbox' : 'radio';
            $.each(data, function (i, v) {
                choices.push(
                    `<label>
                      <input type='${elemType}' name='${v.questionId}' value='${v.id}' id='${v.id}' />
                      ${v.choiceText}
                     </label>`
                );
            });
            if(typeof callback === 'function') {
                callback(choices);
            }
        },
        error: function(){
        }
    });
}
const$examQuestions=$(“#考试题”),
输出=[];
加载问题($examId,函数(问题){
为了(让我来讨论一下){
$switchQuestion.append($('').val(问题[i].id).text(数字(i)+1));
loadChoices(问题[i]。id,问题[i]。多答案,函数(选项){
输出推送(
`${(数字(i)+1)}.${问题[i].问题描述}
${choices.join(“”)}`
);
});
}
});
output.join(“”);
$examQuestions.innerHTML=输出;
函数加载选项(id、isMulti、回调){
让选择=[];
$.ajax({
url:location.protocol+'/'+location.host+'/在线考试考官'+'/choices/'+id,
数据类型:“json”,
键入:“GET”,
成功:功能(数据、状态){
让elemType=isMulti?'checkbox':'radio';
$。每个(数据、功能(i、v){
选择。推(
`
${v.choiceText}
`
);
});
if(回调类型==='function'){
回调(选择);
}
},
错误:函数(){
}
});
}

如果要替换div中的所有内容,请执行以下操作:

$examQuestions.html(output)
否则,还可以使用jQuery的
append
方法,如:

$examQuestions.append(output)

$examQuestions.html(output)@Pavlo抱歉,这是一个错误,我已经更正了。仍然错误,
innerHTML
在jQuery中不存在,请使用
.html()
代替标题。我也尝试了.html()和.append(),但仍然没有显示在页面上。有了这些,当我执行console.log($examQuestions)时,数组不会添加到div元素对不起,您能给我显示$switchQuestion和loadChoices吗?
$examQuestions.append(output)