簡體   English   中英

在Java測驗中得分

[英]Score in a Javascript Quiz

對於一個學校項目,我正在嘗試通過JavaScript創建測驗。 我們問基本的公民問題,然后根據分數顯示一條消息,前提是“應該投票”。 例如,如果您有10個問題中有3個出現問題,則會顯示一條消息,說“開國元勛不滿意”;如果10個問題中有7個問題,則會顯示“如果需要,請繼續投票”。 我相信我有放射狀的按鈕和得分計數器向下的拍子,但是我在最后階段遇到了麻煩。 即基於得分的不同消息。 我相信我可能必須添加其他功能,但是其他人告訴我我不需要。 無論如何,代碼在任何幫助下都將不勝感激。

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>test</title>
<style type="text/css">
.a1 {
}
</style>
<script type="text/javascript">
var score = 0;
function fsubmit() 
{
    var correct1 = document.getElementById("a1")
    if (correct1.checked === true) 
    {
        score ++;
    }
    var correct2 = document.getElementById("b4")
    if (correct2.checked === true) 
    {
        score ++; 
    }           
}
// And here
</script>
</head>
<body>
Question 1:  What is the Surpreme law of the land?
<table width="200" border="1">
  <tbody>
    <tr>
      <td><input type="radio" name="input_1"  id="a1" value="a1" tabindex="1"> U.S Constitution </td>

      <td><input type="radio" name="input_2" id="a2" value="a2" tabindex="2"> Declaration of Independence</td>
    </tr>
  </tbody>
</table>
<table width="200" border="1">
  <tbody>
    <tr> </tr>
    <tr>
      <td><input type="radio" name="input_3" id="a3" value="a3" tabindex="3">Bill of Rights</td>
      <td><input type="radio" name="input_4" id="a4" value="a4" tabindex="4">Mayflower Compact</td>
    </tr>
  </tbody>
</table>

Question 9:  What are the first ten amendments to the U.S Constitution?
<table width="200" border="1">
  <tbody>
    <tr>
      <td><input type="radio" name="input_5"  id="b1" value="b1" tabindex="5"> Oprah's Favorite Things</td>

      <td><input type="radio" name="input_6" id="b2" value="b2" tabindex="6"> The list of Ten</td>
    </tr>
  </tbody>
</table>
<table width="200" border="1">
  <tbody>
    <tr> </tr>
    <tr>
      <td><input type="radio" name="input_7" id="b3" value="b3" tabindex="7">The Ten Commondments</td>
      <td><input type="radio" name="input_8" id="b4" value="b4" tabindex="8">The Bill of Rights</td>
    </tr>
  </tbody>
</table>

<input type="Submit" name="btnsubmit" id="btnsubmit" value="submit"
onclick="fsubmit()"/>
</body>
</html>

您可以在HTML中的“提交”按鈕下方添加一個空段:

<p id="message"></p>

然后在腳本中設置段落的內容-這將需要在fsubmit函數中,以便在用戶單擊按鈕時執行:

var message = "Bad luck";
if (score >= 7) {
  message = "Great job!";
}
document.getElementById("message").innerHTML = message;

約瑟夫·厄爾(Joseph Earl)的回答是正確的,但如果得分很高,則可以考慮使用switch語句,而數組或對象可能會比這更好。

<script>
    var messages = {
        6: "Bad luck",
        7: "Great job!"
    }
    document.getElementById("message").innerHTML = messages[score];
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM