這是在Udemy上課程的一個測驗的表單,利用 radio 的方式來選擇答案,會判斷每一題都要選擇,否則會 alert 提示訊息。 比對選取的答案後會有分數計算後,塞回原本 form 表單的上面顯示得分數。 這邊form用了onsubmit="return submitAnswers()",代表submit送出後會回傳執行submitAnswer()後的結果。
2. 功能
第一題中的選項全部的 name 屬性為name="q1",第二題全部選項皆為name="q2",以此類推,value 則是一樣用 a, b, c, d 來表示個別選項的 value。
<section> <divid="results"></div> <formname="quizForm"onsubmit="return submitAnswers()"> <h3>1. In which HTML element do we put in JavaScript code?</h3> <inputtype="radio"name="q1"value="a"id="q1a">a. <js><br> <inputtype="radio"name="q1"value="b"id="q1b">b. <script><br> <inputtype="radio"name="q1"value="c"id="q1c">c. <body><br> <inputtype="radio"name="q1"value="d"id="q1d">d. <link><br>
<h3>2. Which HTML attribute is used to reference an external JavaScript file?</h3> <inputtype="radio"name="q2"value="a"id="q2a">a. src<br> <inputtype="radio"name="q2"value="b"id="q2b">b. rel<br> <inputtype="radio"name="q2"value="c"id="q2c">c. type<br> <inputtype="radio"name="q2"value="d"id="q2d">d. href<br>
<h3>3. How would you write "Hello" in an alert box?</h3> <inputtype="radio"name="q3"value="a"id="q3a">a. msg("Hello");<br> <inputtype="radio"name="q3"value="b"id="q3b">b. alertBox("Hello");<br> <inputtype="radio"name="q3"value="c"id="q3c">c. document.write("Hello");<br> <inputtype="radio"name="q3"value="d"id="q3d">d. alert("Hello");<br>
<h3>4. JavaScript is directly related to the "Java" programming language</h3> <inputtype="radio"name="q4"value="a"id="q4a">a. True<br> <inputtype="radio"name="q4"value="b"id="q4b">b. False<br>
<h3>5. A variable in JavaScript must start with which special character</h3> <inputtype="radio"name="q5"value="a"id="q5a">a. @<br> <inputtype="radio"name="q5"value="b"id="q5b">b. $<br> <inputtype="radio"name="q5"value="c"id="q5c">c. #<br> <inputtype="radio"name="q5"value="d"id="q5d">d. No Special Character<br> <br> <br> <inputtype="submit"value="Submit Answers"> </form> </section>
// Get User Input var q1 = document.forms["quizForm"]["q1"].value; var q2 = document.forms["quizForm"]["q2"].value; var q3 = document.forms["quizForm"]["q3"].value; var q4 = document.forms["quizForm"]["q4"].value; var q5 = document.forms["quizForm"]["q5"].value;