我怎么能用javascript实现在线测试

时间:2015-07-02 21:53:27

标签: javascript html ajax webserver

我有一个包含小javascript代码的网页,可以为学生提供测试服务。但是测试实际上是硬编码到页面中,我想要某种导入方法,其中显示的页面可以读取文本或xml文档中的问题。我正在考虑使用ajax,但是ajax需要运行某种类型的Web服务器才能解释'get'或'post'方法。有没有办法可以通过运行网络服务器来做到这一点。

这是html代码

[[UITabBar appearance] setTintColor:[UIColor whiteColor]];
var pos = 0,
  test, test_status, question, choice, choices, chA, chB, chC, correct = 0;
var questions = [
  ["What is 10 + 4?", "12", "14", "16", "B"],
  ["What is 20 - 9?", "7", "13", "11", "C"],
  ["What is 7 x 3?", "21", "24", "25", "A"],
  ["What is 8 / 2?", "10", "2", "4", "C"]
];

function _(x) {
  return document.getElementById(x);
}

function renderQuestion() {
  test = _("test");
  if (pos >= questions.length) {
    test.innerHTML = "<h2>You got " + correct + " of " + questions.length + " questions correct</h2>";
    _("test_status").innerHTML = "Test Completed";
    pos = 0;
    correct = 0;
    return false;
  }
  _("test_status").innerHTML = "Question " + (pos + 1) + " of " + questions.length;
  question = questions[pos][0];
  chA = questions[pos][1];
  chB = questions[pos][2];
  chC = questions[pos][3];
  test.innerHTML = "<h3>" + question + "</h3>";
  test.innerHTML += "<input type='radio' name='choices' value='A'> " + chA + "<br>";
  test.innerHTML += "<input type='radio' name='choices' value='B'> " + chB + "<br>";
  test.innerHTML += "<input type='radio' name='choices' value='C'> " + chC + "<br><br>";
  test.innerHTML += "<button onclick='checkAnswer()'>Submit Answer</button>";
}

function checkAnswer() {
  choices = document.getElementsByName("choices");
  for (var i = 0; i < choices.length; i++) {
    if (choices[i].checked) {
      choice = choices[i].value;
    }
  }
  if (choice == questions[pos][4]) {
    correct++;
  }
  pos++;
  renderQuestion();
}
window.addEventListener("load", renderQuestion, false);
div#test {
  border: #000 1px solid;
  padding: 10px 40px 40px 40px;
}

1 个答案:

答案 0 :(得分:0)

如何使用

之类的内容在页面中导入问题及其答案

querystring可以包含一组参数,例如生成查询字符串时指定的timestamp参数, 使用md5的数字签名

请求returnJavaScript.html时。该文件将包含一个处理查询字符串的javscript函数,并验证时间戳是否太旧。那么规则应该是这样的: 如果没有有效的查询字符串,则html文件不返回任何内容或基本无用的信息。 如果md5签名有效,但时间戳已经过去,则不再返回任何内容 如果md5签名有效,并且未传递时间戳(时间限制),则返回问题所需的javascript。

通过这种方式,它使得查看答案变得更加困难。

相关问题