本文实例讲述了jQuery ajax动态生成table的方法。分享给大家供大家参考,具体如下:
$(function(){
ajaxT();
});
function ajaxT(){
$.ajax({
type:"POST",
dataType: "json",
url:"UserInfoServlet",
data:"doaction=userList",
success:function(data){
createShowingTable(data);
}
}
);
}
//动态的创建一个table
function createShowingTable(data) {
var tableStr = "
tableStr = tableStr
+ "
"+"
序号"+"
"+"
用户姓名"+"
工号"+"
职位"+"
创建时间"+"
";var len = data.length;
for ( var i = 0; i < len; i++) {
tableStr = tableStr + "
"+"
"+ (i+1) + ""+"
"+"
"+ data[i].realName + ""+ "
"+ data[i].userNo + ""+ "
"+ data[i].position + ""+"
"+data[i].regTime+""+"
";}
if(len==0){
tableStr = tableStr + "
"+"
"+ 暂无记录 + ""+"
";}
tableStr = tableStr + "
";//添加到div中
$("#tableAjax").html(tableStr);
}
希望本文所述对大家jQuery程序设计有所帮助。