1. 程式人生 > 實用技巧 >如何迴圈遍歷json資料到table

如何迴圈遍歷json資料到table

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6     <script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.js"></script>
 7     <style type="text/css">
 8         
 9     </style>
10 </
head> 11 <body> 12 <table id="myTable"> 13 <thead> 14 <tr> 15 <th>代號</th> 16 <th>城市</th> 17 <th>附加數字</th> 18 </tr> 19 </thead> 20 <tbody></tbody> 21 </table> 22 </body> 23
</html> 24 <script type="text/javascript"> 25 var data = [{name:'6101',value:'北京市',age:'11'}, {name:'6102',value:'天津市',age:'11'}, {name:'6103',value:'上海市',age:'22'}]; 26 $.each(data,function(index,item){ 27 var $tr = $('<tr>'); 28 $.each(item,function(name,val){
29 var $td = $('<td>').html(val); 30 $tr.append($td); 31 }); 32 $('#myTable tbody').append($tr); 33 }); 34 </script>