0

我添加了警报以测试脚本是否正常工作,一旦我删除了它所做的函数,但是一旦我添加了函数,html 甚至都不会显示警报。我尝试将代码加载到不同的文件中并在头部和主体中调用它,由于某种原因,代码甚至不会加载得更少,我可以让按钮在最后工作。

    <!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>
4

5 回答 5

1

在第 88 行:

if(get.q6.value.match(/^age$/i))

你错过)了你的javascript...

于 2013-10-18T09:40:04.987 回答
1

试试这个:- http://jsfiddle.net/aiioo7/fDDCW/

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.");
    }
于 2013-10-18T09:42:18.753 回答
0

你在)这里错过了一个:

if(get.q6.value.match(/^age$/i)

将其更改为:

if(get.q6.value.match(/^age$/i))

于 2013-10-18T09:41:39.493 回答
0
if (get.q6.value.match(/^age$/i) {

这个喜欢缺少一个结束)

试试这个:

if (get.q6.value.match(/^age$/i)) {
于 2013-10-18T09:44:24.857 回答
0

要找出您的 JavaScript 代码有什么问题,请使用try-catch以下方法 -

<script>
     try
     {
       /*some JS code*/
     }
     catch(foo)//Use any variable in place of foo
     {
       alert(foo);
     }
</script>

这绝对不会纠正错误,但可以帮助您找到错误所在。虽然这不是一个答案,但您可以随时随地在全球范围内遵循这种方法。

于 2013-10-18T09:50:57.387 回答