初学js,用它来做了一个在线测试的demo,有兴趣的来看看。

xiao7cn 2007-01-01 10:24:56
<html>
<head>
<title>测试系统--Powered by FriedChicken</title>
<style type="text/css">body,td,th {
font-size: 14px;
}
</style>
<script language="javascript">
var answer=new Array(); //问题的标准答案
var wrong=new Array();

function addAnswer(_answer){ //输入标准答案
answer=_answer.toUpperCase().split(" ");
}

function totalScoring(){
wrong.length=0;
var score=0;
for(i=0;i<answer.length;i++){
var _score=0; //该题的得分
var t=document.getElementsByName('question'+(i+1)); //获得该题的所有选项

var bad_answer=false;
var no_check=true;

for(j=0;j<t.length;j++){ //逐个检查选项是否被选中
if(t[j].checked){
no_check=false;
if(answer[i].indexOf(String.fromCharCode("A".charCodeAt(0)+j))==-1){//如果选错,不给分
bad_answer=true;
break;
}
}
}
if(!bad_answer && !no_check){ //如果选中的选项都属于正确答案,则继续检查是否漏选。
_score=8; //如果完全正确,拿8分
for(j=0;j<answer[i].length;j++){
if(t[answer[i].charAt(j).charCodeAt(0)-"A".charCodeAt(0)].checked==false){//如果还有正确答案没有选的
_score=4; //可以拿4分
wrong[wrong.length]=i; //记录下题号
break;
}
}
}
else{
wrong[wrong.length]=i; //记录下答错的题号
}
score+=_score;
}
//打印出得分和答错题目的答案
var display="你的得分是"+score+"\n\n";
for(i=0;i<wrong.length;i++){
display+=("第"+(wrong[i]+1)+"题的正确答案是:"+answer[wrong[i]]+"\n");
}
document.getElementById("score_display").innerText=display;
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<script language="javascript">addAnswer("A B C D A B C D ABCD ABC AB A");</script><!--在输入正确答案,用空格分开-->
<table width="50%" border="1" align="center" cellpadding="10" cellspacing="5" bordercolor="#FFCC99" bgcolor="#FFFFCC">
<tr>
<td><p>你好,如下问题只有一个选项是正确答案。</p>
<p>回答正确给8分,答错不得分。</p></td>
</tr>
<tr>
<td width="21%">1、测试题描述.................<br>
<input type="radio" name="question1">
A. 选项一<br>
<input type="radio" name="question1">
B. 选项二<br>
<input type="radio" name="question1">
C. 选项三<br>
<input type="radio" name="question1">
D. 选项四</td>
</tr>
<tr>
<td>2、测试题描述.................<br>
<input type="radio" name="question2">
A. 选项一<br>
<input type="radio" name="question2">
B. 选项二<br>
<input type="radio" name="question2">
C. 选项三<br>
<input type="radio" name="question2">
D. 选项四</td>
</tr>
<tr>
<td>3、测试题描述.................<br>
<input type="radio" name="question3">
A. 选项一<br>
<input type="radio" name="question3">
B. 选项二<br>
<input type="radio" name="question3">
C. 选项三<br>
<input type="radio" name="question3">
D. 选项四</td>
</tr>
<tr>
<td>4、测试题描述.................<br>
<input type="radio" name="question4">
A. 选项一<br>
<input type="radio" name="question4">
B. 选项二<br>
<input type="radio" name="question4">
C. 选项三<br>
<input type="radio" name="question4">
D. 选项四</td>
</tr>
<tr>
<td>5、测试题描述.................<br>
<input type="radio" name="question5">
A. 选项一<br>
<input type="radio" name="question5">
B. 选项二<br>
<input type="radio" name="question5">
C. 选项三<br>
<input type="radio" name="question5">
D. 选项四</td>
</tr>
<tr>
<td>6、测试题描述.................<br>
<input type="radio" name="question6">
A. 选项一<br>
<input type="radio" name="question6">
B. 选项二<br>
<input type="radio" name="question6">
C. 选项三<br>
<input type="radio" name="question6">
D. 选项四</td>
</tr>
<tr>
<td>7、测试题描述.................<br>
<input type="radio" name="question7">
A. 选项一<br>
<input type="radio" name="question7">
B. 选项二<br>
<input type="radio" name="question7">
C. 选项三<br>
<input type="radio" name="question7">
D. 选项四</td>
</tr>
<tr>
<td>8、测试题描述.................<br>
<input type="radio" name="question8">
A. 选项一<br>
<input type="radio" name="question8">
B. 选项二<br>
<input type="radio" name="question8">
C. 选项三<br>
<input type="radio" name="question8">
D. 选项四</td>
</tr>
<tr>
<td><p>你好,如下问题至少有一个选项是正确答案。</p>
<p>回答完全正确给8分,只答对部分选项给4分,只要有一项答错不得分。</p></td>
</tr>
<tr>
<td>9、测试题描述.................<br>
<input type="checkbox" name="question9">
A. 选项一<br>
<input type="checkbox" name="question9">
B. 选项二<br>
<input type="checkbox" name="question9">
C. 选项三<br>
<input type="checkbox" name="question9">
D. 选项四</td>
</tr>
<tr>
<td>10、测试题描述.................<br>
<input type="checkbox" name="question10">
A. 选项一<br>
<input type="checkbox" name="question10">
B. 选项二<br>
<input type="checkbox" name="question10">
C. 选项三<br>
<input type="checkbox" name="question10">
D. 选项四</td>
</tr>
<tr>
<td>11、测试题描述.................<br>
<input type="checkbox" name="question11">
A. 选项一<br>
<input type="checkbox" name="question11">
B. 选项二<br>
<input type="checkbox" name="question11">
C. 选项三<br>
<input type="checkbox" name="question11">
D. 选项四</td>
</tr>
<tr>
<td>12、测试题描述.................<br>
<input type="checkbox" name="question12">
A. 选项一<br>
<input type="checkbox" name="question12">
B. 选项二<br>
<input type="checkbox" name="question12">
C. 选项三<br>
<input type="checkbox" name="question12">
D. 选项四</td>
</tr>
<tr>
<td><div align="right">
<input type="button" name="Submit" value="交卷" style="width:100px; height:27; background:#FF6633; color:#FFFFFF" onClick="totalScoring();">
</div></td>
</tr>
<tr>
<td id="score_display">完成后,请点击<交卷>按钮,看看你得了多少分。</td>
</tr>
</table>
</body>

</html>
---------------------
用addAnswer方法设置题目答案(只能用字母)
...全文
265 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiao7cn 2007-01-04
  • 打赏
  • 举报
回复
亮点是对答错的题,列出正确答案. :)

利用xml干什么呢?
liuph3000 2007-01-02
  • 打赏
  • 举报
回复
初学能做出这样的页面,不错
xiehuiqi 2007-01-02
  • 打赏
  • 举报
回复
利用xml是好选择

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧