2016-10-08 158 views
1

Ajax代碼:不在AJAX中顯示警報視圖?

$.ajax({ 
       type:"post", 
       url:"<?php echo base_url()?>index.php/myad/filter", 
       data:{checkbox:checked}, 
       success:function(data){ 
       data = $.parseJSON(data); 
       var list = ''; 
       var cates = ''; 
       var base_url = "<?php echo base_url();?>"; 
       console.log(data); 
       for(var i = 0; i <= data.length; i++){ 
        list =+ '<div class="col-sm-12"><div class="product-view row" style="border-bottom:1px solid #eee;margin-bottom:20px;padding:20px 0px 20px 0px;background:#f1f1f1"><div class="col-sm-3 col-md-3 col-lg-3"> <div class="large-image"> <img alt="#" src="'+base_url+'/uploads/'+data[i]['checkbox'][0]['Image']+'"><div class="image-title"><span class="icon-thumbs-up" onclick="thumb('+data[i]['UniqueID']+',this)" style="font-size:24px;"></span></div></div></div><div class="col-sm-6 col-md-6 col-lg-6"> <div class="product-label"><h4>'+data[i]['FullName']+', '+data[i]['Area']+'</h4><h5 style="font-size:14px;width: 100%;text-overflow: ellipsis;overflow: hidden;white-space: nowrap;"><span class="icon-calendar"></span> '+data[i]['SaleDate']+'</h5><h5 style="font-size:14px;width: 100%;text-overflow: ellipsis;overflow: hidden;white-space: nowrap;"><span class="icon-clock"></span> '; 
        var starttime = data[i]['StartTime']; 
        var endtime = data[i]['EndTime']; 
        var times=''; 
        for(var r = 0; r < starttime.length; r++){ 
        times+=''+starttime[r]+'-'+endtime[r]+' '; 
        } 
        list+=times+'</h5><div data-balloon-length="fit" data-balloon="'+data[i]['Address1']+'" data-balloon-pos="up" ><h5 style="font-size:14px;width: 100%;text-overflow: ellipsis;overflow: hidden;white-space: nowrap;"><span class="icon-home"></span> '+data[i]['Address1']+'</h5></div><div data-balloon-length="fit" data-balloon="'+data[i]['description']+'" data-balloon-pos="up" ><h5 style="font-size:14px;width: 100%;text-overflow: ellipsis;overflow: hidden;white-space: nowrap;"><span class="icon-file"></span> '+data[i]['description']+'</h5></div></div></div><div class="col-sm-3 col-md-3 col-lg-3"><div class="product-label"><h4>CATEGORY</h4>'; 
        for(var j = 0;j < data[i]['checkbox'].length; j++){ 
        cates+='<h5 style="font-size:14px">'+data[i]['checkbox'][j]['Product']+'</h5>'; 
        } 
        list+=cates+'</div></div></div></div>'; 
        alert("for loop is end"); 
       }; 
       alert('loop is end'); 
       } 
      }); 
      } 

它循環顯示的警報end.but不顯示環的警報到底該如何解決呢?提前致謝,並對任何語法錯誤感到抱歉。

回答

0

it displaying alert of a for loop is end.but not displaying alert of a loop is end

這是因爲你的數據變量在惡劣的使用方式:

for(var i = 0; i <= data.length; i++){ 

此行應該是:

for(var i = 0; i < data.length; i++){ 

如果您的數據的長度是「data.length 「你無法獲取位置data.length的元素。

數組元素騙入區間:0,1,2 ....長度-1

在你的情況,當你嘗試獲得:

var starttime = data[i]['StartTime']; 

其中i = data.length ,你會得到未定義的。 這意味着,下一個循環:

for(var r = 0; r < starttime.length; r++){ 

將拋出一個異常:

.... Uncaught TypeError: Cannot read property 'length' of undefined