html中的Javascript不工作,甚至没有警报
我添加了警报来测试脚本是否工作,一旦我删除了它所做的函数,但是一旦我添加了函数,html甚至不会显示警报。我试着将代码加载到另一个文件中,并在头部、身体中调用它,由于某些原因,代码甚至不会加载更少的内容。我可以让最后的按钮工作html中的Javascript不工作,甚至没有警报,javascript,html,Javascript,Html,我添加了警报来测试脚本是否工作,一旦我删除了它所做的函数,但是一旦我添加了函数,html甚至不会显示警报。我试着将代码加载到另一个文件中,并在头部、身体中调用它,由于某些原因,代码甚至不会加载更少的内容。我可以让最后的按钮工作 <!DOCTYPE html> <html> <head> <title> Astronomy Quiz </title> </head> <b
<!DOCTYPE html>
<html>
<head>
<title> Astronomy Quiz </title>
</head>
<body>
<div>
<script>
alert("Quiz");
function quiz() {
var grade = 0;
var get = document.getElementById("quiz");
if (get.q1[1].checked) {
grade += 1;
} else if (!get.q1[0].checked) {
alert("Please answer the first question.");
return;
}
if (get.q2[0].checked) {
grade += 1;
} else if (!get.q2[1].checked) {
alert("Please answer the second question.");
return;
}
var check = 0;
var gradeCheck = 0;
if (get.q3[1].checked) {
check += 1;
gradeCheck += 1;
}
for (var i = 0; i < 4; i++) {
if (get.q3[i].checked && i != 1) {
check += 1;
gradeCheck = 0;
}
}
if (check == 0) {
alert("Please answer the third question.");
return;
}
grade += gradeCheck;
check = 0;
gradeCheck = 0;
if (get.q4[3].checked) {
check += 1;
gradeCheck += 1;
}
for (var i = 0; i < 4; i++) {
if (get.q4[i].checked && i != 3) {
check += 1;
gradeCheck = 0;
}
}
if (check == 0) {
alert("Please answer the fourth question.");
return;
}
grade += gradeCheck;
if (get.q5.value.match(/^galaxy$/i)) {
grade += 1;
}
if (get.q5.value == "") {
alert("PLease answer the fifth question.");
return;
}
if (get.q6.value.match(/^age$/i)) {
grade += 1;
}
if (get.q6.value == "") {
alert("PLease answer the sixth question.");
return;
}
alert("Your grade is " + grade + " / 6.");
}
</script>
<center>
<h1> Astronomy Quiz </h1>
</center>
<h3> True / False </h3>
<form id = "quiz">
<label><b>1)</b> According to Kepler the orbit of the earth is a circle with
the sun at the center.
<input type = "radio" name = "q1" value = "True" />
True
<input type = "radio" name = "q1" value = "False" />
False</label>
<br>
<br>
<label><b>2)</b> Ancient astronomers did consider the heliocentric model of
the solar system but rejected it because they could not detect parallax.
<input type = "radio" name = "q2" value = "True" />
True
<input type = "radio" name = "q2" value = "True" />
False</label>
<br>
<h3> Multiple Choice </h3>
<b>3)</b> The total amount of energy that a star emits is directly related
to its
<br>
<input type = "checkbox" name = "q3" value = "a" />
a) surface gravity and magnetic field
<br>
<input type = "checkbox" name = "q3" value = "b" />
b) radius and temperature
<br>
<input type = "checkbox" name = "q3" value = "c" />
c) pressure and volume
<br>
<input type = "checkbox" name = "q3" value = "d" />
d) location and velocity
<br>
<br>
<b>4)</b> Stars that live the longest have
<br>
<input type = "checkbox" name = "q4" value = "a" />
a) high mass
<br>
<input type = "checkbox" name = "q4" value = "b" />
b) high temperature
<br>
<input type = "checkbox" name = "q4" value = "c" />
c) lots of hydrogen
<br>
<input type = "checkbox" name = "q4" value = "d" />
d) small mass
<br>
<h3> Fill in the Blank </h3>
<label><b>5)</b> A collection of a hundred billion stars, gas, and dust is
called a
<input type = "text" name = "q5" value = "" size = "15" />
.</label>
<br>
<br>
<label><b>6)</b> The inverse of the Hubble's constant is a measure of the
<input type = "text" name = "q6" value = "" size = "15" />
of the universe.</label>
<br>
<br />
<input type = "button" value = "Grade" onclick = "quiz()" />
<input type = "reset" name = "Clear" value = "Clear" />
</form>
</div>
</body>
</html>
天文测验
警惕(“测验”);
函数测验(){
var等级=0;
var get=document.getElementById(“测验”);
if(get.q1[1]。选中){
等级+=1;
}如果(!get.q1[0]。已选中),则为else{
警惕(“请回答第一个问题”);
返回;
}
if(get.q2[0]。选中){
等级+=1;
}如果(!get.q2[1]。已选中),则为else{
警惕(“请回答第二个问题”);
返回;
}
var检查=0;
var gradeCheck=0;
if(get.q3[1]。选中){
检查+=1;
等级检查+=1;
}
对于(变量i=0;i<4;i++){
if(get.q3[i].checked&&i!=1){
检查+=1;
等级检查=0;
}
}
如果(检查==0){
警惕(“请回答第三个问题”);
返回;
}
等级+=等级检查;
检查=0;
等级检查=0;
if(get.q4[3]。选中){
检查+=1;
等级检查+=1;
}
对于(变量i=0;i<4;i++){
if(get.q4[i].checked&&i!=3){
检查+=1;
等级检查=0;
}
}
如果(检查==0){
警惕(“请回答第四个问题”);
返回;
}
等级+=等级检查;
if(get.q5.value.match(/^galaxy$/i)){
等级+=1;
}
如果(get.q5.value==“”){
警惕(“请回答第五个问题”);
返回;
}
if(get.q6.value.match(/^age$/i)){
等级+=1;
}
如果(get.q6.value==“”){
警惕(“请回答第六个问题”);
返回;
}
警惕(“你的成绩是“+成绩+”/6。”);
}
天文测验
对/错
1) 根据开普勒的说法,地球的轨道是一个带有
太阳在中心。
真的
假的
2)古代天文学家确实考虑了日心模式。
但太阳系拒绝了它,因为他们无法检测视差。
真的
假的
选择题
3) 恒星发出的总能量与此直接相关
到
a) 表面重磁场
b) 半径和温度
c) 压力和体积
d) 位置和速度
4) 寿命最长的星星有
a) 高质量
b) 高温
c) 大量的氢
d) 小质量
填空
5) 1000亿颗恒星、气体和尘埃的集合正在形成
称为
.
6) 哈勃常数的倒数是测量
宇宙的一部分。
第88行:
if(get.q6.value.match(/^age$/i))
您在javascript中遗漏了)
。您遗漏了)
此处:
if(get.q6.value.match(/^age$/i)
将此更改为:
如果(get.q6.value.match(/^age$/i))
请尝试以下方法:-
JS:-
alert("Quiz");
function quiz() {
var grade = 0;
var get = document.getElementById("quiz");
if (get.q1[1].checked) {
grade += 1;
} else if (!get.q1[0].checked) {
alert("Please answer the first question.");
return;
}
if (get.q2[0].checked) {
grade += 1;
} else if (!get.q2[1].checked) {
alert("Please answer the second question.");
return;
}
var check = 0;
var gradeCheck = 0;
if (get.q3[1].checked) {
check += 1;
gradeCheck += 1;
}
for (var i = 0; i < 4; i++) {
if (get.q3[i].checked && i != 1) {
check += 1;
gradeCheck = 0;
}
}
if (check == 0) {
alert("Please answer the third question.");
return;
}
grade += gradeCheck;
check = 0;
gradeCheck = 0;
if (get.q4[3].checked) {
check += 1;
gradeCheck += 1;
}
for (var i = 0; i < 4; i++) {
if (get.q4[i].checked && i != 3) {
check += 1;
gradeCheck = 0;
}
}
if (check == 0) {
alert("Please answer the fourth question.");
return;
}
grade += gradeCheck;
if (get.q5.value.match(/^galaxy$/i)) {
grade += 1;
}
if (get.q5.value == "") {
alert("PLease answer the fifth question.");
return;
}
if (get.q6.value.match(/^age$/i)) {
grade += 1;
}
if (get.q6.value == "") {
alert("PLease answer the sixth question.");
return;
}
alert("Your grade is " + grade + " / 6.");
}
警报(“测验”);
函数测验(){
var等级=0;
var get=document.getElementById(“测验”);
if(get.q1[1]。选中){
等级+=1;
}如果(!get.q1[0]。已选中),则为else{
警惕(“请回答第一个问题”);
返回;
}
if(get.q2[0]。选中){
等级+=1;
}如果(!get.q2[1]。已选中),则为else{
警惕(“请回答第二个问题”);
返回;
}
var检查=0;
var gradeCheck=0;
if(get.q3[1]。选中){
检查+=1;
等级检查+=1;
}
对于(变量i=0;i<4;i++){
如果(get.q3[i])已选中
if (get.q6.value.match(/^age$/i) {
if (get.q6.value.match(/^age$/i)) {
<script>
try
{
/*some JS code*/
}
catch(foo)//Use any variable in place of foo
{
alert(foo);
}
</script>