简体   繁体   English

JavaScript行为不符合预期。 无法确定原因。

[英]Javascript not behaving as expected. Unable to identify cause.

in an attempt to make practical use of the skills I am learning on my web development course I am trying to create a website about the Vikings for my partner's Primary school class. 为了实际利用我在网络开发课程中学习的技能,我试图为我的伴侣的小学班级创建一个有关维京人的网站。

I have managed to get the HTML and CSS as I want it, but I'm struggling a little with the Javascript. 我已经设法获得所需的HTML和CSS,但是我在Javascript方面有些挣扎。 it all looks fine to my mind but doesn't run as intended. 在我看来,这一切看起来不错,但并没有按预期运行。

I have a quiz and a submit button. 我有一个测验和一个提交按钮。 When clicked this button will reference a "checkresults" function in my .js file. 单击此按钮时,将在我的.js文件中引用“ checkresults”函数。

This should then calculate a result between 0 - 3 and post this result into the HTML page. 然后,应该计算出0-3之间的结果,并将结果发布到HTML页面中。 I have designed the box the results will show in to be invisible until the "Submit" button is clicked. 我设计了在单击“提交”按钮之前结果将不会显示的框。 However, when ran the results box appears for only a second before disappearing and I cannot figure out why. 但是,当运行时,结果框仅显示一秒钟,然后消失,我无法弄清原因。

any help or advice would be very much appreciated! 任何帮助或建议,将不胜感激!

//JAVASCRIPT// // // JAVASCRIPT

function checkresults() {
var question1 = document.quiz.question1.value;
var question2 = document.quiz.question2.value;
var question3 = document.quiz.question3.value;
var correct = 0;
if (question1 == "793") {
correct++;
}
if (question2 == "Shield") {
correct++;
}
if (question3 == "1066") {
correct++;
}

var message = ["You're a real Viking!", "Not bad but you can do better!", 
"Odin would not be pleased with your effort!"];
var range;

if (correct < 1) {
range = 2;
}
if (correct > 0 && correct < 3) {
range = 1;
}
if (correct > 2) {
range = 0;
}

document.getElementById("afterSubmit").style.visibility = "visible"

document.getElementById("message").innerHTML = message[ramge];
document.getElementById("correct").innerHTML = "You got " + correct + " 
correct!";
}

//HTML//
<form id="quiz" name="quiz">
<p>When did the Vikings first invade Britain?</p>
<input type="radio" id="mc" name="question1" value="1066" />1066<br />
<input type="radio" id="mc" name="question1" value="793" />793<br />
<input type="radio" id="mc" name="question1" value="411" />411<br />
<input type="radio" id="mc" name="question1" value="1999" />1999<br />

<p>what did every man need before he was allowed to go Viking?</p>
<input type="radio" id="mc" name="question2" value="Shield" />Shield<br />
<input type="radio" id="mc"name="question2" value="Sword" />Sword<br />
<input type="radio" id="mc"name="question2" value="Cloak" />Cloak<br />
<input type="radio" id="mc" name-"question2" value="Gold" />Gold<br />

<p>when did the Viking age end?</p>
<input type="radio" id="mc" name="question3" value="793" />793<br />
<input type="radio" id="mc" name="question3" value="1999" />1999<br />
<input type="radio" id="mc" name="question3" value="1066" />1066<br />
<input type="radio" id="mc" name="question3" value="1500" />1500<br />

<input type="submit" id="button" value="Lets see how you did!" onclick = 
"checkresults();">
</form>

<div id="afterSubmit">
<p id="message"></p>
<p id="correct"></p>

//CSS//
#afterSubmit {
visibility: hidden;
border-color: red;
border-style: solid;
border-width: 5px;
}

Your page is refreshing. 您的页面令人耳目一新。

The best way to change this would be to move the function to the form onsubmit event. 更改此错误的最好方法是将函数移到表单onsubmit事件。

//Remove the onclick
<input type="submit" id="button" value="Lets see how you did!" onclick="checkresults();">

Add the function and return false to the event on the form, so it cancels submission 添加函数并向表单上的事件返回false,因此取消提交

//Add the onsubmit, notice the return false, so it cancels submission
<form id="quiz" name="quiz" onsubmit="checkresults();return false;">

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

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