Home  >  Article  >  Web Front-end  >  Example showing JS implementing a simple multiple-choice assessment system

Example showing JS implementing a simple multiple-choice assessment system

巴扎黑
巴扎黑Original
2018-05-30 09:42:382446browse

This article will share with you the example code of a simple multiple-choice assessment system implemented in js. It is very good and has reference value. Friends who need it can refer to it

Includes content: JS encapsulation form, JS verification form

It is said to be an evaluation system, but it feels like it can only be regarded as a small demo. It is very watery, has no database, and only uses JS to make a simple multiple-choice evaluation system.

----- -------------------------------------------------- --------------------------

1. Design ideas

Form encapsulation:

[1] Since it is submitted using JS encapsulation, there is no need for form tags

[2] Place multiple input tags as input items

【3】Write JS to obtain input items and submit them to another page through get method

Verification form (display results)

【1】Get the parameters passed in by get

【2】Parse through JS

【3】Display to the corresponding location

----------------------- -------------------------------------------------- -------

##2. The reference source code is as follows

request.html

<html>
<head>
  <title>考试系统</title>
  <meta http-equiv="accept-charset" charset="utf-8">
  <script src="jquery.min.js"></script>
  <script type="text/javascript">
    function getjson() {
      var radio = new Array();
      for (var i = 1; i <= 5; i++) {//获取radio的值
        var radio_name = new String("radio_" + i);
        radio[i - 1] = $(&#39;input:radio[name=&#39; + radio_name + &#39;]:checked&#39;).val()
      }
      for (var i = 1; i <= 2; i++) {//获取checkbox的的输入
        var checkbox_name = new String("checkbox_" + i);
        var chk_value = [];
        $(&#39;input:checkbox[name=&#39; + checkbox_name + &#39;]:checked&#39;).each(function () {
          chk_value.push($(this).val());
        });
        radio[i + 4] = "";//置为空
        for (var j = 0; j < chk_value.length; j++) {
          radio[i + 4] = radio[i + 4] + chk_value[j];
        }
      }
      //数组转json串
      var json = JSON.stringify(radio);
      return json;
    }
    function my_confirm() {
      var json = getjson();
      var msg = "您真的答案是:" + json + ",是否确认提交";
      if (confirm(msg) == true) {
        window.location.href = "result.html?radio=" + 5 + "checkbox=" + 2 + "&json=" + json;
      } else {
        return false;
      }
    }
    $(function () {
      var m = 1;
      var s = 10;
      setInterval(function () {
        if (m >= 0) {
          if (s < 10) {
            $(&#39;#time&#39;).html("剩余时间:" + m + &#39;:0&#39; + s);
          } else {
            $(&#39;#time&#39;).html("剩余时间:" + m + &#39;:&#39; + s);
          }
          s--;
          if (s < 0) {
            s = 59;
            m--;
          }
          if (m == 0 && s < 1) {
            window.location.href = "result.html?radio=" + 5 + "checkbox=" + 2 + "&json=" + getjson();
          }
        }
      }, 1000)
    })
  </script>
</head>
<body>
<h3 style="float: left">2016--2017学年期末测试题</h3>
<p id="time" style="color:red;float: right;margin: 12px 20px 0 0;padding: 0 0 0 0;font-size: xx-large"></p>
<br/><br/><br/>
<hr/>
<h4>一、单选题(每题12分,满分60分)</h4>
1.当方法遇到异常又不知如何处理时,下列() 做法是正确的。<br>
<input type="radio" name="radio_1" value="A">A、捕获异常<br>
<input type="radio" name="radio_1" value="B">B、抛出异常<br>
<input type="radio" name="radio_1" value="C">C、声明异常<br>
<input type="radio" name="radio_1" value="D">D、嵌套异常<br>
2.下列说法错误的是() <br>
<input type="radio" name="radio_2" value="A">A、在java中一个类被声明为final类型,表示该类不能被继承。<br>
<input type="radio" name="radio_2" value="B">B、当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,这叫引用传递。<br>
<input type="radio" name="radio_2" value="C">C、一个类不能既被声明为 abstract,又被声明为final。<br>
<input type="radio" name="radio_2" value="D">D、方法的覆盖(Overriding)和重载(Overloading)是Java多态性的表现,他们没有区别。<br>
3.下列创建数组的方法哪个是错误的? <br>
<input type="radio" name="radio_3" value="A">A、Date[] arr = new Date[5];<br>
<input type="radio" name="radio_3" value="B">B、Date arr[] = new Date[];<br>
<input type="radio" name="radio_3" value="C">C、Date arr[][] = new Date[4][5];<br>
<input type="radio" name="radio_3" value="D">D、Date arr[][] = new Date[4][];<br>
4.在读文件Employee.txt 时,可以直接使用该文件作为参数的类是() <br>
<input type="radio" name="radio_4" value="A">A、BufferedReader<br>
<input type="radio" name="radio_4" value="B">B、FileInputStream<br>
<input type="radio" name="radio_4" value="C">C、DataOutputStream<br>
<input type="radio" name="radio_4" value="D">D、DataInputStream<br>
5.下列关于线程的说法中,错误的是? <br>
<input type="radio" name="radio_5" value="A">A、线程必须通过方法start() 来启动。<br>
<input type="radio" name="radio_5" value="B">B、线程创建后,其优先级是可以改变的。<br>
<input type="radio" name="radio_5" value="C">C、实现Runnable接口或者从Thread类派生的线程类没有区别。<br>
<input type="radio" name="radio_5" value="D">D、当对象用synchronized 修饰时,表明该对象在任一时刻只能由一个线程访问。<br>
<br/>
<h4>二、多选题(每题20分,满分40分,错选、少选、多选不得分)</h4>
6.下列说法正确的是() <br>
<input type="checkbox" name="checkbox_1" value="A">A、在java中一个类被声明为final类型,表示该类不能被继承。<br>
<input type="checkbox" name="checkbox_1" value="B">B、当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,这叫引用传递。<br>
<input type="checkbox" name="checkbox_1" value="C">C、一个类不能既被声明为 abstract,又被声明为final。<br>
<input type="checkbox" name="checkbox_1" value="D">D、方法的覆盖(Overriding)和重载(Overloading)是Java多态性的表现,他们没有区别。<br>
7.当方法遇到异常又不知如何处理时,下列() 做法是不正确的。<br>
<input type="checkbox" name="checkbox_2" value="A">A、捕获异常<br>
<input type="checkbox" name="checkbox_2" value="B">B、抛出异常<br>
<input type="checkbox" name="checkbox_2" value="C">C、声明异常<br>
<input type="checkbox" name="checkbox_2" value="D">D、嵌套异常<br>
<hr/>
<input type="button" onclick="my_confirm()" value="考试完成">
</body>
</html>

---- -------------------------------------------------- --------------------------

result.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>考试结果</title>
  <script src="jquery.min.js"></script>
  <script>

    //获取url中的参数
    function getUrlParam(name) {
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
      var r = window.location.search.substr(1).match(reg); //匹配目标参数
      if (r != null) return unescape(r[2]);
      return null; //返回参数值
    }
    function showResult() {
      var answer = ["B", "D", "B", "B", "D", "ABC", "ACD"];//标准答案
      var answer_score = [12, 12, 12, 12, 12, 20, 20];//答案的分数
      var user_answer = JSON.parse(getUrlParam("json"));//获取用户答案
      var radio_num = parseInt(getUrlParam("radio"));//获取单选个数
      var checkbox_num = parseInt(getUrlParam("checkbox"));//获取多选个数
      var radio_result = 0;//单选分数
      var checkbox_result = 0;//多选分数
      var radio_right_num = 0;//单选答对个数
      var checkbox_right_num = 0;//多选答对个数
      var result = 0;//总分数
      var user_answer_result = new Array();//用户没到题的答题情况
      for (var i = 0; i < user_answer.length; i++) {
        if (user_answer[i] == answer[i]) {
          if (i < radio_num) {
            radio_result = radio_result + answer_score[i];
            radio_right_num++;
          } else {
            checkbox_result = checkbox_result + answer_score[i];
            checkbox_right_num++;
          }
          user_answer_result[i] = "正确";
        } else {
          user_answer_result[i] = "错误";
        }
      }
      result = checkbox_result + radio_result;
      //结果展示
      var show_result1;
      var show_result2;
      var show_result3;
      var show_result4;
      var show_result5;
      var show_result6;
      show_result1 = "你的答案结果为:";
      for (var i = 0; i < user_answer.length; i++) {
        show_result1 = show_result1 + (i + 1) + ":" + user_answer_result[i] + "; ";
      }
      show_result2 = "总题目个数:" + user_answer.length;
      show_result3 = "答对单选题题目个数:" + radio_right_num + "; 得分:" + radio_result;
      show_result4 = "答对多选题题目个数:" + checkbox_right_num + "; 得分:" + checkbox_result;
      show_result5 = "答错题目个数:" + (user_answer.length - radio_right_num - checkbox_right_num);
      show_result6 = " 本次考试总成绩为:" + result;
      $("p#show_result1").html(show_result1);
      $("p#show_result2").html(show_result2);
      $("p#show_result3").html(show_result3);
      $("p#show_result4").html(show_result4);
      $("p#show_result5").html(show_result5);
      $("p#show_result6").html(show_result6);
    }
  </script>
</head>
<body>
<h2>考试结束!</h2>
<hr/>
<input type="button" onclick="showResult()" value="查看结果">
<p id="show_result1">
<p>
<hr/>
<p id="show_result2"></p>
<p id="show_result3"></p>
<p id="show_result4"></p>
<p id="show_result5"></p>
<hr/>
<p id="show_result6"></p>
</body>
</html>

The result is as shown below:


Example showing JS implementing a simple multiple-choice assessment system

The above is the detailed content of Example showing JS implementing a simple multiple-choice assessment system. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn