怎样用jquery把一个List里面的对象的属性,依次填入到一个table里面啊
比如我的table是<table><thead><tr><th>学号</th><th>姓名</th><th>年龄</th></tr></thead><tbody><tr>...
比如我的table是
<table>
<thead>
<tr>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
我在js文件里面,有这样一个回调函数,要把list里面的对象(student)的属性(id, name, age)填入到html的td中...请问怎样做到啊!!哭啊~而且因为有很多studnet,所以应该还要动态增加行....求助啊~~!!
function result(list){
for(var index = 0; index < list.length; index ++)
{
//是这样吗。。然后怎么写啊T_T
}
} 展开
<table>
<thead>
<tr>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
我在js文件里面,有这样一个回调函数,要把list里面的对象(student)的属性(id, name, age)填入到html的td中...请问怎样做到啊!!哭啊~而且因为有很多studnet,所以应该还要动态增加行....求助啊~~!!
function result(list){
for(var index = 0; index < list.length; index ++)
{
//是这样吗。。然后怎么写啊T_T
}
} 展开
展开全部
list是返回json格式的?假设list格式如下
list = [{"id":"1","name":"A","age":20},{"id":"2","name":"B","age":21},{"id":"3","name":"C","age":22}];
最简单的就是
for(var index = 0,l = list.length;index<l;index++){
$("#students tbody").append("<tr><td>"+ list[index].id +"</td><td>"+ list[index].name+"</td><td>"+ list[index].age +"</td></tr>");
}
students 是table的id
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<script language="javascript" src="/js/jquery-1.4.4.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function (){
var list = [{"id":"1","name":"A","age":20},{"id":"2","name":"B","age":21},{"id":"3","name":"C","age":22}];
for(var index = 0,l = list.length;index<l;index++){
$("#students tbody").append("<tr><td>"+ list[index].id +"</td><td>"+ list[index].name+"</td><td>"+ list[index].age +"</td></tr>");
}
})
</script>
<body>
<table id="students">
<thead>
<tr>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>
list = [{"id":"1","name":"A","age":20},{"id":"2","name":"B","age":21},{"id":"3","name":"C","age":22}];
最简单的就是
for(var index = 0,l = list.length;index<l;index++){
$("#students tbody").append("<tr><td>"+ list[index].id +"</td><td>"+ list[index].name+"</td><td>"+ list[index].age +"</td></tr>");
}
students 是table的id
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<script language="javascript" src="/js/jquery-1.4.4.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function (){
var list = [{"id":"1","name":"A","age":20},{"id":"2","name":"B","age":21},{"id":"3","name":"C","age":22}];
for(var index = 0,l = list.length;index<l;index++){
$("#students tbody").append("<tr><td>"+ list[index].id +"</td><td>"+ list[index].name+"</td><td>"+ list[index].age +"</td></tr>");
}
})
</script>
<body>
<table id="students">
<thead>
<tr>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询