js循环json输出,jquery循环json输出,动态从json导入表格

页面

<!DOCTYPE html>
<html>


<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/jquery-2.0.0.min.js"></script>
<script type="text/javascript">
var areadata = {
province: [],
city: [],
district: []
}


function initAreaData() {
var dataroot = "file/readTest.json";
$.getJSON(dataroot, function(data) {
areadata.province = data.province;
areadata.city = data.city;
areadata.district = data.district;
alert(data.province);


// 普通for循环
for(var i = 0; i < data.province.length; i++) {
alert(data.province[i].name);
};


// jquery的each循环
$.each(data.province, function() {
alert(this.name);
});


// while循环
var i = 0;
while(data.province.length > i) {
alert(data.province[i].name);
++i;
}
});
};
</script>
</head>


<body>
<input type="button" id="_1301471656903021_panel" value="打印" οnclick="initAreaData();" />
</body>


</html>

json数据

{
"province": [
{
"name": "安徽省",
"code": "340000"
}, {
"name": "北京市",
"code": "110000"
}
],
"city": [
{
"name": "安庆市",
"code": "340800"
}, {
"name": "蚌埠市",
"code": "340300"
}
],
"district": [
{
"name": "埇桥区",
"code": "341302"
}, {
"name": "浉河区",
"code": "411502"
}
]
}


填充表格数据

<!DOCTYPE html>
<html>


<head>
<meta charset="UTF-8">
<title></title>
<script src="js/jquery-2.0.0.min.js" type="text/javascript" charset="utf-8"></script>
</head>


<body>
<table border=" cellspacing=" " cellpadding=" ">
<thead>
<tr>
<th>产品</th>
<th>付款日期</th>
<th>状态</th>
</tr>
</thead>
<tbody id="thedata">
<tr>
<th>手机</th>
<th>2013-09-12</th>
<th>快递中</th>
</tr>
</tbody>
</table>
</body>
<script type="text/javascript">

var dataroot = "file/tabledata.json";
$.getJSON(dataroot, function(data) {

// jquery的each循环
$.each(data, function() {
var thedata = $("#thedata");
var trData = "<tr><th>"+ this.name +"</th><th>"+ this.date +"</th><th>"+ this.stuae +"</th></tr>";
//alert(trData);
thedata.append(trData);
});
});
</script>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值