简体   繁体   English

我的测验应用程序未显示测验选项

[英]Quiz options not displaying for my quiz app

I'm developing a quiz app and there is two main function:我正在开发一个测验应用程序,有两个主要功能:

  1. fetchData获取数据

This function will fetch questions from a API and display everything to the webpage.此函数将从 API 获取问题并将所有内容显示到网页上。

  1. isCorrect是正确的

This function will check if the answer is correct typed by the user.此函数将检查用户输入的答案是否正确。 If it is, add 1 to the score variable.如果是,则将分数变量加 1。 But when the first function runs, it doesn't show me anything.但是当第一个函数运行时,它没有显示任何内容。 My guess is that the option is not properly displayed or something.我的猜测是该选项未正确显示或其他原因。 Here is my first function's code:这是我的第一个函数的代码:

async function fetchData() {
  var getData = await fetch(url);
  var toJS = await getData.json();
  answer_container = toJS.results[0].correct_answer;
  var container = [];
  for (var i = 0; i < toJS.results[0].incorrect_answers.length; i++) {
    container.push(toJS.results[0].incorrect_answers[i]);
  }
  container.push(toJS.results[0].correct_answer);
  container.sort(func);

  function func(a, b) {
    return 0.5 - Math.random();
  }
  container.forEach(function(choices) {
    html_container.push(`
<option value=${choices}>
${choices}
</option>
`)
    console.log(choices);
  });
  document.getElementById('choice').add(html_container.join());
  if (toJS.results[0].type === 'boolean') {
    document.getElementById('type').innerHTML =
      `This question is a ${toJS.results[0].category} question <br>
It is a true/false question<br>
Difficulty level: ${toJS.results[0].difficulty} <br>
Question: ${toJS.results[0].question}<br>
`;
  } else {
    document.getElementById('type').innerHTML =
      `This question is a ${toJS.results[0].category} question <br>
It is a ${toJS.results[0].type} choice question <br>
Difficulty level: ${toJS.results[0].difficulty} <br>
Question: ${toJS.results[0].question}<br>
`;
  }
  document.getElementById('answer_element').style.display = "none";
  document.getElementById('answer_element').innerHTML = "The answer to this question is " + toJS.results[0].correct_answer;
}
fetchData();

I got this error on the console:我在控制台上收到此错误:

Uncaught (in promise) TypeError: Failed to execute 'add' on 'HTMLSelectElement': The provided value is not of type '(HTMLOptGroupElement or HTMLOptionElement)'.未捕获(承诺)类型错误:无法在“HTMLSelectElement”上执行“添加”:提供的值不是“(HTMLOptGroupElement 或 HTMLOptionElement)”类型。

But I clearly added my options to a select element with an id of choice.但是我清楚地将我的选项添加到了带有选择 id 的 select 元素中。

Problem问题

I'm assuming line 23 is the error since it was the only add method shown.我假设line 23是错误,因为它是唯一显示的add方法。 The error is caused by passing a string ( html_container.join() ) to the add method, but it is expecting an HTMLOptGroupElement or HTMLOptionElement .该错误是通过使一个字符串(引起html_container.join()add方法,但它期待一个HTMLOptGroupElementHTMLOptionElement To fix this issue, you have at least two options: createElement and innerHTML .要解决此问题,您至少有两个选项: createElementinnerHTML


Solution 1: createElement解决方案 1: createElement

Use document.createElement('option') when creating the option element as follows:创建选项元素时使用document.createElement('option')如下:

var option = document.createElement('option');
option.value = choices;
option.innerHTML = choices;
document.getElementById('choice').add(option);

Solution 2: innerHTML (only)解决方案 2: innerHTML (仅)

Use .innerHTML to add the string to the select element's inner HTML as follows:使用.innerHTML将字符串添加到 select 元素的内部 HTML 中,如下所示:

// use += to append
document.getElementById('choice').innerHTML += html_container.join();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM