简体   繁体   English

如何使用 CSS hidden 属性重定向到新页面?

[英]How can I use the CSS hidden property to redirect to a new page?

so I've coded a questionnaire and I'm running into an issue.所以我编写了一份问卷,但遇到了一个问题。 When a user clicks submit the questionnaire the results are displayed near the submit button I was hoping for them to be redirected to a new page (where results are displayed) by using the CSS hidden property, but I don't understand how I can do that and also for some reason after submitting there is an 'undefined' option displaying under the choices, how can I get rid of that?当用户点击提交问卷时,结果会显示在提交按钮附近并且由于某种原因,提交后在选项下显示了一个“未定义”选项,我该如何摆脱它? I'd really appreciate it if someone can help me solve my issues, thank you!如果有人能帮我解决我的问题,我将不胜感激,谢谢!

I linked the IDE for my project if that's easier: https://repl.it/@AS11RA/Forest-Firefighters-Website#start%20questionnaire.js如果更容易的话,我为我的项目链接了 IDE: https : //repl.it/@AS11RA/Forest-Firefighters-Website#start%20questionnaire.js

Heres the startquestionnaire.Js file:下面是 startquestionnaire.Js 文件:

function buttonClicked(button) {
  button.style.visibility = 'hidden';
  startSurvey();
  console.log('Survey started.');
}

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;
    debugger;
    for (let [key, value] of Object.entries(ourQuestions[i].answers)) {
      document.forms[i].innerHTML +=
        '</div><div class="answer"><input name="q1" value="' +
        value +
        '" id="value4" type="checkbox" />' +
        `${key}: '${value}'`; //use a string literal, makes a dev's life very easy
      ('<br/>');
    }
    document.getElementById('questions').innerHTML += '</form><br/><br/>';
  }

  document.getElementById('questions').innerHTML +=
    '<button class="button" onclick="solveSurvey()">Submit Questionnaire</button>';
}

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: 'Strong Wind Patterns',
    },
    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',
  },
  {
    question: 'What 3 things are needed to start a fire?',
    answers: {
      a: 'Matches, oxygen, wood',
      b: 'Air and sunlight',
      c: 'Fuel, heat, oxygen',
      d: 'Fuel, oxygen, wood',
    },
    correctAnswer: 'c',
  },
  {
    question: 'Which one of these is NOT a type of forest fire?',
    answers: {
      a: 'Crown',
      b: 'Firework',
      c: 'Surface',
      d: 'Ground ',
    },
    correctAnswer: 'b',
  },
  {
    question: 'What was the cause of the Amazon forest fires in 2019?',
    answers: {
      a: 'Deforestation for agriculture usage',
      b: 'Fireworks',
      c: 'Unattended campfire',
      d: 'Arson',
    },
    correctAnswer: 'a',
  },
  {
    question:
      'Which one of these names are NOT an alternate name for forest fires?',
    answers: {
      a: 'Wildfires',
      b: 'Shrub fires',
      c: 'Natural fireworks',
      d: 'Brushfires ',
    },
    correctAnswer: 'c',
  },
  {
    question: 'How many forest fires occurred in 2019?',
    answers: {
      a: '25 653',
      b: '50 477',
      c: '45 809',
      d: '89 431',
    },
    correctAnswer: 'b',
  },
];

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.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.getElementById('questions').innerHTML +=
    'Correct answers: ' + correct;
}

Here's the questionnaire.HTML file:这是问卷.HTML 文件:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Forest Firefighters: Questionnaire</title>
    <link href="survey style.css" rel="stylesheet" type="text/css" />
    <link href="main style.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="script.js" type="text/javascript"></script>
    <script src="start questionnaire.js" type="text/javascript"></script>
  </head>
  
  <body>
    <!--────────────────Header───────────────-->
      <header>
          <nav> 
            <ul class="nav-bar"><div class="bg"></div>
                <li><a class="nav-link" href="about.html">About</a></li>
                <li><a class="nav-link" href="index.html">Home</a></li>
                <li><a class="nav-link active" href="questionnaire.html">Questionnaire</a></li>
              <li><a class="nav-link" href="learning more.html">Learning more</a></li>
          <li><a class="nav-link" href="">Ways you can help</a></li>
            </ul>
          </nav>
      </header>

    <main>
        <!--─────────────────Home────────────────-->
        <div id="home">
            <div class="filter"></div>

        <!--────questionnaire button─────-->        
        <button class="button" onclick="buttonClicked(this)">Start Questionnaire</button>
        <spacer></spacer>
        <div id="questions"></div>
        <spacer></spacer>
        <spacer></spacer>
        </div>
    
      
          <div class="citing">
        <a class="citing-link" href=questionnaire.html">Image Source: https://phys.org/news/2019-11-countries-forest.html</a>
      <div>      
      </main>  

    <!--─────────────────Footer────────────────-->
      <footer class="copyright">© 2020 Amber, Aatiqah, Selina</footer>
  </body>

</html>

If we store your results in its own variable, we then have 2 options right off the bat.如果我们将您的结果存储在自己的变量中,那么我们立即有 2 个选项。

var results = `Correct answers: ${correct}`;
  

Easiest way would be to display it in an alert function, let the browser handle it.最简单的方法是在警报功能中显示它,让浏览器处理它。

alert(results);

Another way would be to spawn your own window and edit the html contents of it.另一种方法是生成您自己的窗口并编辑它的 html 内容。

var resultsWin = window.open("about:blank", "Questionnaire Results", "width=200,height=100");
resultsWin.document.write(results);

Keep in mind that the opening of the window will not work in the snippet due to this:请记住,由于以下原因,在代码段中无法打开窗口:

js:253 Blocked opening 'about:blank' in a new window because the request was made in a sandboxed frame whose 'allow-popups' permission is not set.

The reason for the undefined was because your appending of the answers was using the inside for loop j but it existed in just the outer one, with no access to j undefined的原因是因为您对答案的附加使用了内部 for 循环j但它仅存在于外部循环中,无法访问j

I went ahead and replaced it to this:我继续将其替换为:

x.innerHTML += `<div class="answer"><input name="q1" value="${ourQuestions[i].correctAnswer}" id="value4" type="radio" />${ourQuestions[i].correctAnswer}</div>`;

 function buttonClicked(button) { button.style.visibility = 'hidden'; startSurvey(); console.log('Survey started.'); } function startSurvey() { var i, j, k; for (i = 0; i < ourQuestions.length; i++) { document.getElementById('questions').innerHTML += '<form id="question">Q' + (i + 1) + ': ' + ourQuestions[i].question; for (let [key, value] of Object.entries(ourQuestions[i].answers)) { document.forms[i].innerHTML += `<div class="answer"><input name="q1" value="${value}" id="value4" type="checkbox" />${key}: '${value}'</div>`; } document.getElementById('questions').innerHTML += '</form><br/><br/>'; } document.getElementById('questions').innerHTML += '<button class="button" onclick="solveSurvey()">Submit Questionnaire</button>'; } 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: 'Strong Wind Patterns', }, 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', }, { question: 'What 3 things are needed to start a fire?', answers: { a: 'Matches, oxygen, wood', b: 'Air and sunlight', c: 'Fuel, heat, oxygen', d: 'Fuel, oxygen, wood', }, correctAnswer: 'c', }, { question: 'Which one of these is NOT a type of forest fire?', answers: { a: 'Crown', b: 'Firework', c: 'Surface', d: 'Ground ', }, correctAnswer: 'b', }, { question: 'What was the cause of the Amazon forest fires in 2019?', answers: { a: 'Deforestation for agriculture usage', b: 'Fireworks', c: 'Unattended campfire', d: 'Arson', }, correctAnswer: 'a', }, { question: 'Which one of these names are NOT an alternate name for forest fires?', answers: { a: 'Wildfires', b: 'Shrub fires', c: 'Natural fireworks', d: 'Brushfires ', }, correctAnswer: 'c', }, { question: 'How many forest fires occurred in 2019?', answers: { a: '25 653', b: '50 477', c: '45 809', d: '89 431', }, correctAnswer: 'b', }, ]; 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; } } } x.innerHTML += `<div class="answer"><input name="q1" value="${ourQuestions[i].correctAnswer}" id="value4" type="radio" />${ourQuestions[i].correctAnswer}</div>`; } //document.getElementById('questions').innerHTML += 'Correct answers: ' + correct; var results = `Correct answers: ${correct}`; // Easiest way would be to display it in an alert function, let the browser handle it. alert(results); // Another way would be to spawn your own window and edit the html contents of it. var resultsWin = window.open("about:blank", "Questionnaire Results", "width=200,height=100"); resultsWin.document.write(results); }
 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>Forest Firefighters: Questionnaire</title> <link href="survey style.css" rel="stylesheet" type="text/css" /> <link href="main style.css" rel="stylesheet" type="text/css" /> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="script.js" type="text/javascript"></script> <script src="start questionnaire.js" type="text/javascript"></script> </head> <body> <!--────────────────Header───────────────--> <header> <nav> <ul class="nav-bar"> <div class="bg"></div> <li><a class="nav-link" href="about.html">About</a></li> <li><a class="nav-link" href="index.html">Home</a></li> <li><a class="nav-link active" href="questionnaire.html">Questionnaire</a></li> <li><a class="nav-link" href="learning more.html">Learning more</a></li> <li><a class="nav-link" href="">Ways you can help</a></li> </ul> </nav> </header> <main> <!--─────────────────Home────────────────--> <div id="home"> <div class="filter"></div> <!--────questionnaire button─────--> <button class="button" onclick="buttonClicked(this)">Start Questionnaire</button> <spacer></spacer> <div id="questions"></div> <spacer></spacer> <spacer></spacer> </div> <div class="citing"> <a class="citing-link" href="questionnaire.html">Image Source: https://phys.org/news/2019-11-countries-forest.html</a> <div> </main> <!--─────────────────Footer────────────────--> <footer class="copyright">© 2020 Amber, Aatiqah, Selina</footer> </body> </html>

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

相关问题 如何判断页面作者是否手动设置了CSS属性? - How can I tell if a CSS property was manually set by page author? 如何使用 URL 参数重定向到页面? - How can I use an URL parameter to redirect to a page? 为什么我不能使用 javascript 将可见性属性更改为隐藏 - Why can't I use javascript to change the visibility property to hidden 我如何重定向到另一个页面并同时调用这个新页面的事件? - How i can redirect to another page and call an event of this new page at the same time? 如何重定向到新页面? - How to redirect to a new page? 我可以使用对象属性值作为CSS属性值吗? - can i use an object property value, as a css property value? 如何通过打开新窗口执行OAuth请求,而不是从当前页面重定向用户? - How can I do OAuth request by open new window, instead of redirect user from current page? 在flask应用程序中,如何根据视图中发现的条件在ajax发布期间重定向到新页面? - In a flask application, how can I redirect to a new page during ajax post based on condition discovered in view? 使用Google登录表单登录后,如何将用户重定向到新页面? - How can I redirect users to a new page after they logged in with the Google login form? 如何使用javascript更改按钮的visible / hidden属性? - How can I change the visible / hidden property of a button with javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM