jquery 通過ajax請(qǐng)求獲取后臺(tái)數(shù)據(jù)顯示在表格上的方法
1、引入bootstrap和jquery的cdn
<link rel="stylesheet" type="text/css" rel="external nofollow" > <script type="text/javascript" src="https://cdn.bootcss.com/jquery/1.4.0/jquery.js"></script>
2、html部分
<table class="table table-bordered" id='tabletest'> <tr> <th>名字</th> <th>開始時(shí)間</th> <th>是否真實(shí)</th> <th>設(shè)備</th> </tr> </table>
3、js部分
1>使用for in
$(function(){
$.ajax({
url:'data.json',
type:'get',
dataType:'json',
success:function(data){
//方法中傳入的參數(shù)data為后臺(tái)獲取的數(shù)據(jù)
for(i in data.data) //data.data指的是數(shù)組,數(shù)組里是8個(gè)對(duì)象,i為數(shù)組的索引
{
var tr;
tr='<td>'+data.data[i].name+'</td>'+'<td>'+data.data[i].startTime+'</td>'+'<td>'+data.data[i].is_true+'</td>'+'<td>'+data.data[i].device+'</td>'
$("#tabletest").append('<tr>'+tr+'</tr>')
}
}
})
})
***注意**** for in 通常用于對(duì)象
遍歷數(shù)組的兩種方法(each,foreach):
$.each(arr,function(index,item){})
arr.forEach(function(item,index))
// arr為數(shù)組 ,index索引,item為當(dāng)前值
2>each方法
$(function(){
$.ajax({
url:'data.json',
type:'get',
dataType:'json',
success:function(data){
$.each(data.data,function(index,item){
var tr;
tr='<td>'+item.name+'</td>'+'<td>'+item.startTime+'</td>'+'<td>'+item.is_true+'</td>'+'<td>'+item.device+'</td>';
$("#tabletest").append('<tr>'+tr+'</tr>')
})
}
})})
總結(jié):獲取對(duì)象屬性的方法:item.name或item['name']
jquery添加節(jié)點(diǎn)方法:
ul.append('<li>'+哈哈+'</li>')
append:在</ul>之前添加li
prepend:在<ul>之后添加li
before:在<ul>之前添加li
after:在</ul>之后添加li
-----延伸----
(1)將數(shù)據(jù)中is_true中的0轉(zhuǎn)換為中文
采用三目運(yùn)算或條件判斷
item.is_true=parseInt(item.is_true)==0?'否':'是' //注意數(shù)據(jù)是string類型需轉(zhuǎn)換,且三目運(yùn)算符返回的是結(jié)果不能寫成item.is_true==0? item.is_true='否': item.is_true='是'
(2)將數(shù)據(jù)中device過濾只顯示冒號(hào)以前的數(shù)據(jù)
item.is_true=parseInt(item.is_true)==0?'否':'是'
var arr=item.device.split(":")
item.device=arr[0]
split()分隔符方法用于把一個(gè)字符串分割成字符串?dāng)?shù)組
4.data.json文件
{
"status": 0,
"data": [
{
"name": "天王蓋地虎",
"startTime": "2017-03-02 00:00",
"is_true":"0",
"device": "SM-C9000:samsung"
},
{
"name": "寶塔鎮(zhèn)河妖",
"startTime": "2017-03-02 00:00" ,
"is_true":"0",
"device": "SM705:smartisan"
},
{
"name": "鋤禾日當(dāng)午",
"startTime": "2017-03-02 00:00" ,
"is_true":"0" ,
"device": "EVA-AL00:HUAWEI"
}
]
}
效果圖:

以上這篇jquery 通過ajax請(qǐng)求獲取后臺(tái)數(shù)據(jù)顯示在表格上的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- jQuery+Ajax+js實(shí)現(xiàn)請(qǐng)求json格式數(shù)據(jù)并渲染到html頁(yè)面操作示例
- jQuery中ajax請(qǐng)求后臺(tái)返回json數(shù)據(jù)并渲染HTML的方法
- jQuery Ajax請(qǐng)求后臺(tái)數(shù)據(jù)并在前臺(tái)接收
- 淺析jQuery Ajax請(qǐng)求參數(shù)和返回?cái)?shù)據(jù)的處理
- JQUERY的AJAX請(qǐng)求緩存里的數(shù)據(jù)問題處理
- jQuery使用ajax跨域請(qǐng)求獲取數(shù)據(jù)
- Jquery使用AJAX方法請(qǐng)求數(shù)據(jù)
相關(guān)文章
jQuery實(shí)現(xiàn)表單步驟流程導(dǎo)航代碼分享
這篇文章主要介紹了jQuery實(shí)現(xiàn)表單步驟流程導(dǎo)航,代碼實(shí)現(xiàn)效果簡(jiǎn)單精致,推薦給大家,有需要的小伙伴可以參考下。2015-08-08
jQuery實(shí)現(xiàn)在HTML文檔加載完畢后自動(dòng)執(zhí)行某個(gè)事件的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)在HTML文檔加載完畢后自動(dòng)執(zhí)行某個(gè)事件的方法,結(jié)合實(shí)例形式分析了document的ready()事件自動(dòng)加載執(zhí)行事件的相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
基于jQuery的一個(gè)擴(kuò)展form序列化到j(luò)son對(duì)象
jQuery沒有直接支持form到j(luò)son的序列化方法,目前網(wǎng)上有一個(gè)實(shí)現(xiàn)是這樣的2010-12-12
jQuery實(shí)現(xiàn)仿路邊燈箱廣告圖片輪播效果
本文給大家介紹的是使用jQuery實(shí)現(xiàn)仿路邊燈箱廣告圖片輪播切換特效,效果非常棒,有需要的小伙伴可以參考下。2015-04-04
jQuery1.3.2 升級(jí)到j(luò)Query1.4.4需要修改的地方
jQuery1.3.2 升級(jí)到 1.4.4 ,需要修改的地方,需要的朋友可以參考下。2011-01-01

