前些日子帮人做了一个在线考试系统,JS代码如下
<body>
<div id="content" style="display: none">
<p style="font-weight: 700"> 1,the questions mentioned above</p>
<p style="font-weight: 700"> 2,the timer counting frome 12-(60seconds*2)to 0 <span style="color: #FF4500"> TimeRemaining:</span><a style="font-weight: 600;" id="countdown"></a></p>
<table id="answer"></table>
<table id="dispute"></table>
<div>
</div>
<div style="margin-left: 100px;margin-top: 20px;"><input type="button" id="submitbtn" value="submit" onclick="Submit()" /></div>
</div>
<div id="AnswerDiv" class="mydiv" style="display: none">
<p>
Your mark is :<span id="mark" style="color: red"></span>
</p>
<div id="correctAnswer" style="display: none">
<p>Correct Answer</p>
<ul id="correctAnswer_ul"></ul>
</div>
<p style="color:blue;display: none" id="allRight">good 100 !!!</p>
</div>
</body>
<script> //timer var timer = 120; var Time_kill; window.onload = function () { // if (confirm("Are you ready for the quiz? you need to submit in 120s.")) { // document.getElementById("content").style.display = "block"; // Quenstion1(); // document.getElementById("countdown").innerHTML = timer; // Time_kill = setInterval("Time_up()", 1000); // } document.getElementById("content").style.display = "block"; Quenstion1(); document.getElementById("countdown").innerHTML = timer; Time_kill = setInterval("Time_up()", 1000); };
//定时器 function Time_up() { if (timer == 0) { clearInterval(Time_kill); alert('The time expires~'); document.getElementById("submitbtn").disabled = "disabled"; return; } timer--; document.getElementById("countdown").innerHTML = timer; } var style1 = []; var style2 = []; var Answers = [ { Id: 1, Title: "Mary, your dress is really beautiful. How is John? ", mptions: { A: "Thank you very much", B: "No, no, John is not bad", C: "Thank you. He is fine", D: "Don’t say that. It’s ugly. John is good" }, Answer: "C", }, { Id: 2, Title: "— Mary, are you and your husband busy this weekend? —. ", mptions: { A: "this weekend we may go to cinema", B: "No, we’re not", C: "Oh, it’s none mf your business", D: "Welcome to mur party" }, Answer: "B", }, { Id: 3, Title: "— You are not the manager here, are you? —. ", mptions: { A: "Yes, I’m not ", B: "No, I am", C: "Yes, I am", D: " No, not at all" }, Answer: "C", }, { Id: 4, Title: " --- How do you do? --- _________ ", mptions: { A: "Fine, thank you.", B: "How do you do?", C: "Not too bad", D: "Very well. " }, Answer: "B", }, { Id: 5, Title: "-- How do you do? Glad to meet you. --- _________ ", mptions: { A: "Fine. How are you?", B: "How do you do? Glad to meet you, too. ", C: "How are you? Thank you!", D: "Nice. How are you? " }, Answer: "B", }, { Id: 6, Title: "--- How are you, Bob? --- _______, Ted", mptions: { A: "How are you?", B: "I'm fine. Thank you", C: "How do you do?", D: "Nice to meet" }, Answer: "B", }, { Id: 7, Title: "______morning she gets up at six.", mptions: { A: "every day", B: "on every", C: "in every", D: "every" }, Answer: "D", }, { Id: 8, Title: "--- How's your mother doing? --- ________ ", mptions: { A: "She is very kind. ", B: "She is very well", C: "She is not very mld.", D: "She is doing shopping now." }, Answer: "B", }, { Id: 9, Title: "--- Thank you for calling. --- ________", mptions: { A: "Don't mention it. ", B: "That's fine.", C: "Nice talking to you.", D: "Call back again. " }, Answer: "C", }, { Id: 10, Title: "— Hello, is that Shanghai Airlines? —_____________ . ", mptions: { A: "Yes, can I help you?", B: "Yes, what do you want? ", C: "Yes, you’re right", D: "Yes, right number" }, Answer: "A", } ]; var TFAnswers = [ { Id: 1, Title: "Address individual customer’s risk is the first important section to risk precaution for the bank.", Answer: "T" }, { Id: 2, Title: "Under the risk-based supervision framework, mffsite review plays an important role in risk identification and early warning. ", Answer: "T" }, { Id: 3, Title: "FX (foreign exchange) risk is generally categorized into FX trading risk and FX structure/mismatching risk.", Answer: "T" }, { Id: 4, Title: "One innovation mf Basel II is to introduce the capital requirements for liquidity risk. ", Answer: "F" }, { Id: 5, Title: "Under Basel II, general provisions mr loan loss reserves can be included in Tier 1 capital.", Answer: "F" } ]; function Quenstion1() { style1 = []; style2 = []; var i = 0; var randoms = []; var random; while (i < 4) {
//随机事件 random = parseInt(10 * Math.random()); if (randoms.length > 0 && randoms.indexOf(random) > -1) { continue; } randoms.push(random); style1.push(Answers[random]); i++; } var randoms1 = []; i = 0; while (i < 2) { random = parseInt(2 * Math.random()); if (randoms1.length > 0 && randoms1.indexOf(random) > -1) { continue; } randoms1.push(random); style2.push(TFAnswers[random]); i++; } var Table1 = document.getElementById("answer"); var Table2 = document.getElementById("dispute"); var html = ''; var t; for (t = 0; t < 4; t++) { html += '<tr id="' + style1[t]['Id'] + '" style="height: 60px;"> <td><p style="font-weight: 600;">• ' + (t + 1) + ':' + style1[t]['Title'] + '</p>'; html += '<p style="padding-left: 30px;">'; for (var mbj in style1[t]["mptions"]) { html += ' <span> <input name="answer' + t + '" type="radio" value=' + mbj + ' />' + mbj + '.' + style1[t]["mptions"][mbj] + '</span>'; } html += '</p></td></tr>'; } var html1 = ""; for (t = 0; t < style2.length; t++) { html1 += '<tr id="' + style2[t]['Id'] + '" style="height: 40px;"><td style="font-weight: 600;">• ' + (t + 5) + ':' + style2[t]["Title"]; html1 += '</td><td style="margin-left:20px;padding-left:70px;"><input type="radio" value="T" name="dispute' + t + '" />T</td><td> <input type="radio" value="F" name="dispute' + t + '" />F</td></tr>'; } Table1.innerHTML = html; Table2.innerHTML = html1; } function Submit() { var n = 0; var get_choose = document.getElementById("answer").getElementsByTagName("tr"); for (var t = 0; t < get_choose.length; t++) { var get_input = get_choose[t].getElementsByTagName("input"); var tf_answer = false; for (var i = 0; i < get_input.length; i++) { if (get_input[i].checked) { tf_answer = true; break; } } if (!tf_answer) { alert("please answer all questions!"); return; } } var get_answers = document.getElementById("dispute").getElementsByTagName("tr"); for (var t = 0; t < get_answers.length; t++) { var get_input = get_answers[t].getElementsByTagName("input"); var tf_answer = false; for (var i = 0; i < get_input.length; i++) { if (get_input[i].checked) { tf_answer = true; break; } } if (!tf_answer) { alert("please answer all questions!"); return; } } document.getElementById("submitbtn").disabled = "disabled"; var correctHtml = ""; //get mark for (var t = 0; t < get_choose.length; t++) { var get_input = get_choose[t].getElementsByTagName("input"); for (var i = 0; i < get_input.length; i++) { if (get_input[i].checked) { for (var m = 0; m < style1.length; m++) { if (style1[m]["Id"] == get_choose[t].id) { if (style1[m]["Answer"] == get_input[i].value) { n += 18; } else { correctHtml += '<li>Question ' + (t+1) + ' : the answer should be <span style="color:red">' + style1[m]["Answer"] + '<span></li>'; } } } } } } for (var t = 0; t < get_answers.length; t++) { var get_input = get_answers[t].getElementsByTagName("input"); for (var i = 0; i < get_input.length; i++) { if (get_input[i].checked) { for (var m = 0; m < style2.length; m++) { if (style2[m]["Id"] == get_answers[t].id) { if (style2[m]["Answer"] == get_input[i].value) { n += 14; } else { correctHtml += '<li>Question ' + (t+5) + ' : the answer should be<span style="color:red">' + style2[m]["Answer"] + '<span></li>'; } } } } } } clearInterval(Time_kill); document.getElementById("correctAnswer_ul").innerHTML = correctHtml; document.getElementById("mark").innerText = n; if (n == 100) { document.getElementById("allRight").style.display = "block"; } else { document.getElementById("correctAnswer").style.display = "block"; }
//打开新的窗口 var mpen = window.open(); mpen.document.write(document.getElementById("AnswerDiv").innerHTML); mpen.document.close(); } </script>