我一直试图寻找这个问题的解决方案,但仍然无法找到它。 HTML正在执行,但Javascript没有执行。我已将其包含在index.html文件中并尝试了外部.js文件。仍然无法让我的javascript执行。还有CSS没有执行的问题。需要一些新的眼睛。感谢名单...
<!doctype html>
<html>
<head>
<title> X-Ray Quiz </title>
<link href = “style.css” rel = “stylesheet”>
<link href = “https://fonts.googleapis.com/css?family=Lato” rel=
“stylesheet”>
</head>
<body>
<h5><font color=orange > Quick Asessment </h5></font>
<form id = "quiz" name = "quiz">
<p> Identify this image </p>
<img src=name.jpg height=30% width=30%><br>
<input id= "textbox" type= "text" name= "question1">
<p> identify this image <p>
<img src= name.jpg height=30% width=30%><br>
<input type= "radio" type= "mc" name= "question2" value= "normal">
normal <br>
<input type= "radio" type= "mc" name= "question2" value= “Choice”>
COPD<br>
<input type= "radio" type= "mc" name= "question2" value= “Choice”>
Choice<br>
<p> Identify this image <p>
<img src= name.jpg height=30% width = 30%><br>
<input type= "radio" type= "mc" name= "question3" value= “Normal”>
Normal <br>
<input type= "radio" type= "mc" name= "question3" value=
“choice”>choice<br>
<input type= "radio" type= "mc" name= "question3" value=
“Asthma”>Asthma<br>
<input id= "button" type= "button" value= "Submit" onClick=
"function();">
</form>
<div id = “after_submit”>
<p id = “number_correct”></p>
main.js
function check () {
var question1 = document.quiz.question1.value;
var question2 = document.quiz.question2.value;
var question3 = document.quiz.question3.value;
var correct = 0;
If (question1 == “normal”) {
correct ++;
}
if (question2 == “COPD”) {
correct ++;
}
if (question3 == “Fibrosis”) {
correct ++;
}
document.getElementById(“after_submit”).style.visibility = “visible”;
document.getElementById(“number_correct”).innerHTML = “You got “ +
correct + “correct.”;
}
var messages = [ “Great job”, “That’s just okay”, “ You really need to
do better!”];
var range;
if (correct <1) {
range =2;
if (correct > 0 && correct<3) {
range = 1;
}
if (correct>2) {
range = 0;
}
document.getElementById(“after_submit”).style.visibility = “visible”;
document.getElementById(“message”).innerHTML = messages[range];
document.getElementById(“number_correct”).innerHTML = “ You got “ +
correct + “ correct. “;
}
答案 0 :(得分:1)
首先,您的JavaScript文件未链接到您的HTML。您可能需要添加:
<script src="main.js"><script>
(或者你的HTML被截断了,结尾了吗?)
在您的HTML中,您没有调用正确的JavaScript函数:
<input id= "button" type= "button" value= "Submit" onClick="function();">
现在,您的JavaScript读取
function check () { ... }
所以它应该是onClick="check();"
,因为这是你的函数的名称。
答案 1 :(得分:0)
代码中有很多错误,所以即使它连接正确,它也不会运行。
例如:
If (question1 == “normal”) {/* ...etc */ }
If
应为小写。这些引号是引用引号“
而不是"
。我不知道所有口译员是否都会扼杀这些口译员,但是我的口译员会这样做。
此外,您在函数correct
中定义check()
,但随后尝试在函数外部访问它,它不再在范围内。这会导致错误。
如果您使用浏览器控制台,则很容易找到所有这些错误 - 它们都列在那里。在Chrome中,它是Ctrl+Shift+J
(Windows / Linux)或Cmd+Opt+J
(Mac)。
答案 2 :(得分:0)
好吧 - 就像其他人说的那样,你的代码中有很多错误。我去尝试尽可能地纠正你的代码,但我也注意到没有message
元素。我补充说,现在该程序正常运行。当然,没有CSS添加到这个片段,所以它看起来有点乏味。希望这对你有用!
function check () {
var question1 = document.quiz.question1.value;
var question2 = document.quiz.question2.value;
var question3 = document.quiz.question3.value;
var correct = 0;
if (question1 == 'normal') {
correct ++;
}
if (question2 == 'COPD') {
correct ++;
}
if (question3 == 'Fibrosis') {
correct ++;
}
document.getElementById('after_submit').style.visibility = 'visible';
document.getElementById('number_correct').innerHTML = 'You got ' + correct + 'correct.';
var messages = [ 'Great job', 'That’s just okay', ' You really need to do better!'];
var range;
if (correct <1) {
range =2;
if (correct > 0 && correct<3) {
range = 1;
}
if (correct>2) {
range = 0;
}
document.getElementById('after_submit').style.visibility = 'visible';
document.getElementById('message').innerHTML = messages[range];
document.getElementById('number_correct').innerHTML = ' You got ' + correct + ' correct. ';
}
}
<!doctype html>
<html>
<head>
<title> X-Ray Quiz </title>
<link href = 'style.css' rel = 'stylesheet'>
<link href = 'https://fonts.googleapis.com/css?family=Lato' rel=
'stylesheet'>
<script src="JS/scripts.js"></script>
</head>
<body>
<h5><font color=orange > Quick Asessment </h5></font>
<form id = "quiz" name = "quiz">
<p> Identify this image </p>
<img src=name.jpg height=30% width=30%><br>
<input id= "textbox" type= "text" name= "question1">
<p> identify this image <p>
<img src= name.jpg height=30% width=30%><br>
<input type= "radio" type= "mc" name= "question2" value= "normal">
normal <br>
<input type= "radio" type= "mc" name= "question2" value= 'Choice'>
COPD<br>
<input type= "radio" type= "mc" name= "question2" value= 'Choice'>
Choice<br>
<p> Identify this image <p>
<img src= name.jpg height=30% width = 30%><br>
<input type= "radio" type= "mc" name= "question3" value= 'Normal'>
Normal <br>
<input type= "radio" type= "mc" name= "question3" value=
'choice'>choice<br>
<input type= "radio" type= "mc" name= "question3" value=
'Asthma'>Asthma<br>
<input id= "button" type= "button" value= "Submit" onclick=
"check();">
</form>
<div id = 'after_submit'>
<p id = 'number_correct'></p>
<p id='message'></p>
</div>
</body>
</html>