最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

jquery 通過ajax請(qǐng)求獲取后臺(tái)數(shù)據(jù)顯示在表格上的方法

 更新時(shí)間:2018年08月08日 09:19:50   作者:cyyy1223  
今天小編就為大家分享一篇jquery 通過ajax請(qǐng)求獲取后臺(tái)數(shù)據(jù)顯示在表格上的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

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è)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

临沭县| 巨鹿县| 车险| 西和县| 阿拉善左旗| 库尔勒市| 宁武县| 施秉县| 昌宁县| 钦州市| 新河县| 鹤壁市| 武威市| 湛江市| 宜兰市| 阳西县| 班戈县| 当涂县| 儋州市| 澳门| 登封市| 怀宁县| 蓬安县| 天等县| 略阳县| 涟水县| 绩溪县| 永年县| 汪清县| 九寨沟县| 乐亭县| 巴南区| 武义县| 林口县| 潜山县| 霸州市| 肇东市| 达拉特旗| 盐池县| 屏边| 呼和浩特市|