【发布时间】:2020-11-22 22:21:04
【问题描述】:
我想构建一个简单易用的基于 Javascript 的测验应用程序(记住简单易用的代码,而不是高级 Javascript)我尽了最大努力,但我被困在这一点上,除了结果之外,一切都运行良好。我想使用 Javascript 在 html 标记中获取结果。这是我的代码:
// Javascript (important I need a solution in this code)
function check(){
// question no1
var score = 0;
var q1WriteAnswer = document.getElementById('q1_o3');
var q1option1 = document.getElementById('q1_o1');
var q1option2 = document.getElementById('q1_o2');
var q1option4 = document.getElementById('q1_o4');
if (q1WriteAnswer.checked==true){
score++
}
else{
alert("wrong answer");
}
// question no2
var score = 0;
var q2WriteAnswer = document.getElementById('q2_o1');
var q2option1 = document.getElementById('q2_o2');
var q2option2 = document.getElementById('q2_o3');
var q2option4 = document.getElementById('q2_o4');
if (q2WriteAnswer.checked==true){
score++
}
else{
alert("wrong answer");
}
// Question no3
var score = 0;
var q3WriteAnswer = document.getElementById('q3_o2');
var q3option1 = document.getElementById('q3_o1');
var q3option2 = document.getElementById('q3_o3');
var q3option4 = document.getElementById('q3_o4');
if (q3WriteAnswer.checked==true){
score++
}
else{
alert("wrong answer");
}
// Question no4
var score = 0;
var q4WriteAnswer = document.getElementById('q4_o4');
var q4option1 = document.getElementById('q4_o1');
var q4option2 = document.getElementById('q4_o2');
var q4option4 = document.getElementById('q4_o3');
if (q4WriteAnswer.checked==true){
score++
}
else{
alert("wrong answer");
}
// Question no5
var score = 0;
var q5WriteAnswer = document.getElementById('q5_o1');
var q5option1 = document.getElementById('q5_o2');
var q5option2 = document.getElementById('q5_o3');
var q5option4 = document.getElementById('q5_o4');
if (q5WriteAnswer.checked==true){
score++
}
else{
alert("wrong answer");
}
// Question no6
var score = 0;
var q6WriteAnswer = document.getElementById('q6_o1');
var q6option1 = document.getElementById('q6_o2');
var q6option2 = document.getElementById('q6_o3');
var q6option4 = document.getElementById('q6_o4');
if (q6WriteAnswer.checked==true){
score++
}
else{
alert("wrong answer");
}
// Questiton No7
var score = 0;
var q7WriteAnswer = document.getElementById('q7_o1');
var q7option1 = document.getElementById('q7_o2');
var q7option2 = document.getElementById('q7_o3');
var q7option4 = document.getElementById('q7_o4');
if (q7WriteAnswer.checked==true){
score++
}
else{
alert("wrong answer");
}
// Question no 8
var score = 0;
var q8WriteAnswer = document.getElementById('q8_o1');
var q8option1 = document.getElementById('q8_o2');
var q8option2 = document.getElementById('q8_o3');
var q8option4 = document.getElementById('q8_o4');
if (q8WriteAnswer.checked==true){
score++
}
else{
alert("wrong answer");
}
// Question No9
var score = 0;
var q9WriteAnswer = document.getElementById('q9_o3');
var q9option1 = document.getElementById('q9_o1');
var q9option2 = document.getElementById('q9_o2');
var q9option4 = document.getElementById('q9_o4');
if (q9WriteAnswer.checked==true){
score++
}
else{
alert("wrong answer");
}
// Question No10
var score = 0;
var q10WriteAnswer = document.getElementById('q10_o1');
var q10option1 = document.getElementById('q10_o2');
var q10option2 = document.getElementById('q10_o3');
var q10option4 = document.getElementById('q10_o4');
if (q10WriteAnswer.checked==true){
score++
}
else{
alert("wrong answer");
}
var ab = document.createElement("h3");
ab.innerText = score;
document.body.appendChild(ab);
}
/* *{
padding: 0px;
margin: 0px;
} */
body{
color: white;
background-color: #222;
background-image: url(images/bg.jpg);
background-repeat: no-repeat;
background-size: cover;
background-position: center;
}
fieldset{
border-radius: 15px;
text-align: center;
}
h3{
background-color: cornsilk;
color: blue;
width: 10vw;
height: 5vh;
text-align: center;
position: relative;
left: 45%;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Quiz Application</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Quiz Application</h1>
<div>
<fieldset>
<p>Who is the founder of Pakistan?</p>
<input type="radio" name="founder" id="q1_o1">Allama Iqbal</radio>
<input type="radio" name="founder" id="q1_o2">Manzoor Kapri</radio>
<input type="radio" name="founder" id="q1_o3">Quaid-e-Azam</radio>
<input type="radio" name="founder" id="q1_o4">Liaquat Ali Jinah</radio>
</fieldset>
</div><br>
<br>
<br>
<div>
<fieldset>
<p>Who is national poet of Pakistan?</p>
<input type="radio" name="national_poet" id="q2_o1">Allama Iqbal</radio>
<input type="radio" name="national_poet" id="q2_o2">Manzoor Kapri</radio>
<input type="radio" name="national_poet" id="q2_o3">Quaid-e-Azam</radio>
<input type="radio" name="national_poet" id="q2_o4">Liaquat Ali Jinah</radio>
</fieldset>
</div><br>
<br>
<br>
<div>
<fieldset>
<p>Who is the writer of national anthem of Pakistan?</p>
<input type="radio" name="anthem_poet" id="q3_o1">Allama Iqbal</radio>
<input type="radio" name="anthem_poet" id="q3_o2">Hafiz Jalindheri</radio>
<input type="radio" name="anthem_poet" id="q3_o3">Quaid-e-Azam</radio>
<input type="radio" name="anthem_poet" id="q3_o4">Liaquat Ali Jinah</radio>
</fieldset>
</div><br>
<br>
<br>
<div>
<fieldset>
<p>what is the national animal of Pakistan?</p>
<input type="radio" name="national_animal" id="q4_o1">Peacock</radio>
<input type="radio" name="national_animal" id="q4_o2">Dog</radio>
<input type="radio" name="national_animal" id="q4_o3">Lion</radio>
<input type="radio" name="national_animal" id="q4_o4">Markhor</radio>
</fieldset>
</div><br>
<br>
<br>
<div>
<fieldset>
<p>Who is the Prime Minister of Pakistan?</p>
<input type="radio" name="pm" id="q5_o1">Imran Khan</radio>
<input type="radio" name="pm" id="q5_o2">Manzoor Kapri</radio>
<input type="radio" name="pm" id="q5_o3">Quaid-e-Azam</radio>
<input type="radio" name="pm" id="q5_o4">Liaquat Ali Jinah</radio>
</fieldset>
</div><br>
<br>
<br>
<div>
<fieldset>
<p>what is the area of pakistan in square km?</p>
<input type="radio" name="pk_sq_km" id="q6_o1">1,96,096 sq km</radio>
<input type="radio" name="pk_sq_km" id="q6_o1">5,05,095 sq km</radio>
<input type="radio" name="pk_sq_km" id="q6_o1">7,96,096 sq km</radio>
<input type="radio" name="pk_sq_km" id="q6_o1">2,96,096 sq km</radio>
</fieldset>
</div><br>
<br>
<br>
<div>
<fieldset>
<p>Who is the first prime minister of Pakistan?</p>
<input type="radio" name="first_PM" id="q7_o1">Liaquat Ali</radio>
<input type="radio" name="first_PM" id="q7_o2">Zulfiqar Ali Bhutto</radio>
<input type="radio" name="first_PM" id="q7_o3">Quaid-e-Azam</radio>
<input type="radio" name="first_PM" id="q7_o4">Manzoor Kapri</radio>
</fieldset>
</div><br>
<br>
<br>
<div>
<fieldset>
<p>Which was the first capital of Pakistan?</p>
<input type="radio" name="first_Capital" id="q8_o1">Karachi</radio>
<input type="radio" name="first_Capital" id="q8_o2">Islamabad</radio>
<input type="radio" name="first_Capital" id="q8_o3">Hydrabad</radio>
<input type="radio" name="first_Capital" id="q8_o4">Quetta</radio>
</fieldset>
</div><br>
<br>
<br>
<div>
<fieldset>
<p>Who is the first woman prime minister of Pakistan?</p>
<input type="radio" name="first_womanPM" id="q9_o1">Shoukat Khanam</radio>
<input type="radio" name="first_womanPM" id="q9_o2">Shamshad Akhtar</radio>
<input type="radio" name="first_womanPM" id="q9_o3">Benazir Bhutto</radio>
<input type="radio" name="first_womanPM" id="q9_o4">Liaquat Ali Jinah</radio>
</fieldset>
</div><br>
<br>
<br>
<div>
<fieldset>
<p>which is the current capital of Pakistan?</p>
<input type="radio" name="founder" id="">Islamabad</radio>
<input type="radio" name="founder" id="">Karachi</radio>
<input type="radio" name="founder" id="">Hydrabad</radio>
<input type="radio" name="founder" id="">Lahore</radio>
</fieldset>
</div><br>
<br>
<br>
<button onclick="check()">Results</button>
<script src="app.js"></script>
</body>
</html>
【问题讨论】:
-
var score = 0;应该只在函数开始时出现一次。通过一直重复它,您将在每个问题评分之前将分数重置为 0。 -
我也这样做了,它现在在我的控制台中显示此消息(未捕获的类型错误:无法读取属性 'checked' of null at check)
标签: javascript html css dom-events javascript-objects