<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>挑战题</title>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<style>
* {
font: 15px "微软雅黑";
line-height: 1.5;
}
input {
margin: 5px 0px;
}
#submit {
margin: 20px 0px 0px 186px
}
#count {
color: red;
}
</style>
</head>
<body>
<span>请输入学生姓名:</span>
<input id="stuName" type="text"></input>
<br/>
<span>请输入学生分数:</span>
<input id="stuScore" type="text"></input>
<br/>
<input id="submit" type="button" value="提交"></input>
<input id="print" type="button" value="打印"></input>
<div>
<p>已经提交了<span id="count"></span>个学生的信息,点击打印按钮将信息打印出来:</p>
<ol id="list">
</ul>
<script>
$(function() {
var counter=0;
$("#count").html(counter);
$("#submit").bind("click", function() {
PrintJson();
});
// 添加json数据
function buildJson() {
var jsonStu = [];
var name=$("#stuName").val(),score=$("#stuScore").val();
$("#submit").bind("click",function(){
addJsonElements(jsonStu,name,score);
$("#stuName").html("");
$("#stuScore").html("");
$("#count").html(counter++);
})
return jsonStu;
}
//向json中添加元素
function addJsonElements(objArr,stuName,stuScore){
objArr.Push({
name:stuName,
score:stuScore
});
}
//打印Json
function PrintJson(){
$.each(buildJson() , function(index, stu) {
$("#list").append("<li>姓名:"+stu.name+" 成績:"+stu.score);
});
}
});
</script>
</div>
</body>
</html>