关于json的循环打印
本篇文章是关于getjson(url,[data],function(data){})方法得到一个json对象;
json里面需要去遍历;然后拼接成html格式,输出。
//异步初始化选课课程清单
function initInfo() {
var stu_id = $("#Label2").text();
//把参数传到InitInfo.ashx一般处理程序中,返回一个data的json对象;
//返回的json:{"ds":[{"cou_id":1,"cou_name":"黑客攻防"},{"cou_id":3,"cou_name":"网络安全"},{"cou_id":5,"cou_name":"asp.net基础"}, //{"cou_id":6,"cou_name":"html基础"}]}
$.getJSON("InitInfo.ashx", { "id": stu_id }, function (data) {
//alert出data的长度
// alert(JSON.stringify(data));
//data里ds下面的个数
//alert(data.ds.length);
//拼接html
$("#tbody").html("");
var strTr = "";
for (var i = 0; i < data.ds.length;i++) {
strTr+= "<tr>";
strTr += "<td>" + data.ds[i].cou_id + "</td>";
strTr += "<td>" + data.ds[i].cou_name + "</td>";
strTr += "<td><a onClick='Modify(" + data.ds[i].cou_id + "," + stu_id + ")'>编辑</a> ";
strTr += "<a onClick=\"return confirm('即将删除该记录,操作不可恢复,确定吗?');\" href='Delete.ashx?couid='" + data.ds[i].cou_id + "&stuId=" + stu_id + ">删除</a></td>";
strTr += "</tr>";
}
$("#tbody").html(strTr)
});
}

浙公网安备 33010602011771号