<script type="text/javascript"> $(document).ready(function () { $.ajax({ type: "get", dataType: "Json", url: "../Ashx/Handler1.ashx", start: function () { alert("開始獲取數據了") }, complete: function () { alert("獲取完了") }, success: function (data) { var t = eval(data); //強制轉換一下json字符串,生成json對象 $.each(t, function (i, n) { var row = $("#template").clone(); //克隆模板,創建一個新數據行 for (attribute in n) { row.find("#" + attribute).html(n[attribute]); //循環json對象的屬性,並賦值到數據行中對應的列,此處列的id就是相應的屬性名稱 } row.appendTo($("#testTable")); }); } }); }); </script>
<table id="testTable" border="1"> <th style="width: 30%">編號</th> <th style="width: 30%">標題</th> <th style="width: 30%">內容</th> <!--數據模板--> <!--其中每一列的id就是對應記錄中的列名--> <tr id="template"> <td id="Id" style="width: 30%"></td> <td id="title" style="width: 30%"></td> <td id="intro" style="width: 30%"></td> </tr> <!--數據模板--> </table>