Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.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 TypeError:“输入错误”;TypeError:函数名不是HtmlButtoneElement.onclick(/:2:54)上的函数;_Javascript_Html_Function_Typeerror - Fatal编程技术网

Javascript TypeError:“输入错误”;TypeError:函数名不是HtmlButtoneElement.onclick(/:2:54)上的函数;

Javascript TypeError:“输入错误”;TypeError:函数名不是HtmlButtoneElement.onclick(/:2:54)上的函数;,javascript,html,function,typeerror,Javascript,Html,Function,Typeerror,我正试图对Javascript进行一次非常简单的调查,但我一直遇到这样的错误“TypeError:startSurvey不是htmlButtoneElement.onclick(/:2:54)的函数”,如果有人能帮我解决这个错误或提供任何进一步的反馈和建议,我将不胜感激 以下是我的HTML代码: <div class="surveysection"> <button onclick="startSurvey()" id="

我正试图对Javascript进行一次非常简单的调查,但我一直遇到这样的错误“TypeError:startSurvey不是htmlButtoneElement.onclick(/:2:54)的函数”,如果有人能帮我解决这个错误或提供任何进一步的反馈和建议,我将不胜感激

以下是我的HTML代码:

<div class="surveysection">
    <button onclick="startSurvey()" id="startSurvey">Start Survey</button>
    <div id="questions"></div> 
</div>

<script>

开始调查
这是我的脚本文件:

var ourQuestions = [
  {
    question:'While naturally occurring wildfires can benefit ecosystems, unnatural blazes started by uncaring and negligent humans can do great harm and cause many deaths. What percentage of wildfires do you think are started by humans?',
    answers: {
      a: '10-15%',
      b: '85-90%',
      c: '45-50%',
      d: '25-30%'
    },
    correctAnswer: 'b'
  },
  {
    question: 'If you have lit a campfire before, how did you extinguish it?',
    answers: {
      a: 'I did not extinguish it and waited for it to die on its own',
      b: 'I extinguished the campfire with a bucket of water and made sure it was fully extinguished.',
      c: 'I have never lit a campfire before.',
      d: 'uhhh'
    },
    correctAnswer: 'b'
  },
  {
    question: 'What are the two most common reasons that forest fires start?',
    answers: {
      a: 'Lightning and human negligence',
      b: 'Spontaneous combustion and erosion',
      c: 'Animals igniting flames and overcrowded bushlands',
      d: 'uhhh'
    },
    correctAnswer: 'a'
  },
  {
    question: 'What time of the year do most forest fires occur?',
    answers: {
      a: 'Summer',
      b: 'Spring',
      c: 'Fall',
      d: 'Winter'
    },
    correctAnswer: 'a'
  },
  {
    question: 'How fast do you think forest fires spread?',
    answers: {
      a: '10.8 km/h',
      b: '6.4 km/h',
      c: '22.2 km/h',
      d: '3.2 km/h'
    },
    correctAnswer: 'a'
  },
  {
    question: 'What do forest fires need in order to burn?',
    answers: {
      a: 'Water',
      b: 'High humidity',
      c: 'Fuel',
      d: 'Clear weather'
    },
    correctAnswer: 'c'
  },
  {
    question: 'What is one of the main toxic gases present in forest fire smoke?',
    answers: {
      a: 'Osmium tetroxide',
      b: 'Disulfur decafluoride',
      c: 'Tungsten hexafluoride ',
      d: 'carbon monoxide'
    },
    correctAnswer: 'd'
  },
  {
    question: 'What natural disasters could be caused as a consequence of a destructive forest fire?',
    answers: {
      a: 'Erosion, flash flooding and landslides',
      b: 'Tornadoes',
      c: 'Snow',
      d: 'Tsunami and earthquakes'
    },
    correctAnswer: 'a'
  },
  {
    question: 'What major factor determines a forest fire’s behaviour?',
    answers: {
      a: 'Amount of water vapour in air',
      b: 'Density of Forests',
      c: 'Wind',
      d: 'Hours of sunlight'
    },
    correctAnswer: 'c'
  }
];

function startSurvey(){
    
    var i;
    var j;
    var k;
    for(i=0; i<ourQuestions.length; i++){
        document.getElementById("questions").innerHTML +='<form id="question">Q'+(i+1)+': '+ ourQuestions[i].question;
        
        for(j=0; j<ourQuestions[i].answers.length; j++){
            document.forms[i].innerHTML += '</div><div class="answer"><input name="q1" value="'+ ourQuestions[i].answers[j] +'" id="value4" type="checkbox" />' + ourQuestions[i].answers[j] + '<br/>';
         }
     document.getElementById("questions").innerHTML +='</form><br/><br/>';
    }
    
    document.getElementById("questions").innerHTML += '<button onclick="solveQuiz()">Solve Quiz</button>';
    
}

function solveSurvey(){
  var x;
  var txt = ' ';
  var i = 0;
  var correct = 0; 
  for(i = 0; i < document.forms.length;i++) { 
    x = document.forms[i]; 
    for(j = 0; j<x.length; j++){
      if(x[j].checked) { 
        correctAnswer = ourQuestions[i].correctAnswer;
        if(x[j].value == ourQuestions[i].answers[correctAnswer]){
          correct += 1;
        }
      }
   }
 }
 document.getElementById("questions").innerHTML += 'Correct answers: '+ correct;
} 

document.forms[i].innerHTML += '</div><div class="answer"><input name="q1" value="'+ ourQuestions[i].answers[j] +'" id="value4" type="radio" />' + ourQuestions[i].answers[j] + '<br/>';
var问题=[
{
问题:“虽然自然发生的野火有利于生态系统,但由不关心和疏忽的人类引发的非自然火灾会造成巨大伤害,并导致许多人死亡。你认为有多大比例的野火是由人类引发的?”,
答复:{
答:10-15%,
b:‘85-90%’,
c:‘45-50%’,
d:‘25-30%’
},
正确答案:“b”
},
{
问题:“如果你以前点燃过篝火,你是如何熄灭的?”,
答复:{
答:“我没有熄灭它,而是等待它自己死去。”,
b:“我用一桶水扑灭了营火,并确保它完全熄灭。”,
c:“我以前从未点过营火。”,
d:‘嗯’
},
正确答案:“b”
},
{
问:“森林火灾发生的两个最常见的原因是什么?”,
答复:{
答:“闪电和人为疏忽”,
b:‘自燃和侵蚀’,
c:“动物点燃火焰和过度拥挤的丛林地带”,
d:‘嗯’
},
正确答案:“a”
},
{
问题:“一年中大多数森林火灾发生在什么时候?”,
答复:{
a:‘夏天’,
b:‘春天’,
c:‘秋天’,
d:‘冬天’
},
正确答案:“a”
},
{
问:“你认为森林火灾蔓延的速度有多快?”,
答复:{
a:‘10.8公里/小时’,
b:‘6.4公里/小时’,
c:‘22.2公里/小时’,
d:‘3.2公里/小时’
},
正确答案:“a”
},
{
问题:“森林火灾需要什么才能燃烧?”,
答复:{
a:‘水’,
b:‘高湿度’,
c:‘燃料’,
d:‘晴朗的天气’
},
正确答案:“c”
},
{
问:“森林火灾烟雾中的主要有毒气体是什么?”,
答复:{
a:‘四氧化锇’,
b:‘十氟化二硫’,
c:‘六氟化钨’,
d:‘一氧化碳’
},
正确答案:“d”
},
{
问题:“破坏性森林火灾可能导致哪些自然灾害?”,
答复:{
答:“侵蚀、山洪暴发和滑坡”,
b:‘龙卷风’,
c:‘雪’,
d:“海啸和地震”
},
正确答案:“a”
},
{
问题:“决定森林火灾行为的主要因素是什么?”,
答复:{
a:‘空气中的水蒸气量’,
b:‘森林密度’,
c:‘风’,
d:‘日照时间’
},
正确答案:“c”
}
];
函数startSurvey(){
var i;
var j;
var-k;

因为(i=0;i看起来您的id和函数是相同的名称,下面我将函数名更改为
start();
)-仅此一项就可能解决您的问题

此外,这里似乎没有定义i:

document.forms[i].innerHTML += '</div><div class="answer"><input name="q1" value="'+ ourQuestions[i].answers[j] +'" id="value4" type="radio" />' + ourQuestions[i].answers[j] + '<br/>';
document.forms[i].innerHTML+=''+ourQuestions[i].answers[j]+'
你可能想解决这个问题

var问题=[
{
问题:“虽然自然发生的野火有利于生态系统,但由不关心和疏忽的人类引发的非自然火灾会造成巨大伤害,并导致许多人死亡。你认为有多大比例的野火是由人类引发的?”,
答复:{
答:10-15%,
b:‘85-90%’,
c:‘45-50%’,
d:‘25-30%’
},
正确答案:“b”
},
{
问题:“如果你以前点燃过篝火,你是如何熄灭的?”,
答复:{
答:“我没有熄灭它,而是等待它自己死去。”,
b:“我用一桶水扑灭了营火,并确保它完全熄灭。”,
c:“我以前从未点过营火。”,
d:‘嗯’
},
正确答案:“b”
},
{
问:“森林火灾发生的两个最常见的原因是什么?”,
答复:{
答:“闪电和人为疏忽”,
b:‘自燃和侵蚀’,
c:“动物点燃火焰和过度拥挤的丛林地带”,
d:‘嗯’
},
正确答案:“a”
},
{
问题:“一年中大多数森林火灾发生在什么时候?”,
答复:{
a:‘夏天’,
b:‘春天’,
c:‘秋天’,
d:‘冬天’
},
正确答案:“a”
},
{
问:“你认为森林火灾蔓延的速度有多快?”,
答复:{
a:‘10.8公里/小时’,
b:‘6.4公里/小时’,
c:‘22.2公里/小时’,
d:‘3.2公里/小时’
},
正确答案:“a”
},
{
问题:“森林火灾需要什么才能燃烧?”,
答复:{
a:‘水’,
b:‘高湿度’,
c:‘燃料’,
d:‘晴朗的天气’
},
正确答案:“c”
},
{
问:“森林火灾烟雾中的主要有毒气体是什么?”,
答复:{
a:‘四氧化锇’,
b:‘十氟化二硫’,
c:‘六氟化钨’,
d:‘一氧化碳’
},
正确答案:“d”
},
{
问题:“破坏性森林火灾可能导致哪些自然灾害?”,
答复:{
答:“侵蚀、山洪暴发和滑坡”,
b:‘龙卷风’,
c:‘雪’,
d:“海啸和地震”
},
正确答案:“a”
},
{
问题:“决定森林火灾行为的主要因素是什么?”,
答复:{
a:‘空气中的水蒸气量’,
b:‘森林密度’,
c:‘风’,
d:‘日照时间’
},
正确答案:“c”
}
];
函数start(){
var i;
var j;
var-k;

对于(i=0;i,这里有一把小提琴来解决您的问题:

将函数更改为
startservey()