2013-02-13 30 views
3

我試圖在某個時間間隔後更改div的內部HTML。 我得到正確的答案,我想用Ajax。 ,但無法替換在使用Ajax響應之後選擇的內部HTML。 什麼是錯我的代碼..用Ajax響應替換div的內部HTML

的Html

 <p class="time ui-li-desc" data-time="2013-02-13 11:30:08" > 
    51 seconds ago<img alt="image" src="images/read.png"></p> 

     <p class="time ui-li-desc" data-time="2013-02-13 11:30:16" > 
    58 seconds ago<img alt="image" src="images/read.png"></p> 
. 
. 
. 
. 
. 
     <p class="time ui-li-desc" data-time="2013-02-13 11:40:08" > 
    10 minute ago<img alt="image" src="images/read.png"></p> 

Ĵ查詢

setInterval(function() { 
      $(".time").each(function(index) { 
       var sendTime= $(this).attr("data-time"); 
       dataString = "sendtime="+sendTime+"&q=convertTime"; 
       $.ajax({ 
        type: "POST", 
        url: "data_handler.php", 
        data: dataString,     
        cache: true, 
        success: function(response) { 
         alert(response); 
         $(this).html(response); 
         //alert(response); 
        } 
       }); 
      }); 
     }, 5000); 
+0

會發生什麼事,當你運行它? – Archer 2013-02-13 10:46:10

回答

6

this是在回調的窗口。使用給每個callback值:

 $(".time").each(function(index , elem) { 
      var sendTime= $(this).attr("data-time"); 
      dataString = "sendtime="+sendTime+"&q=convertTime"; 
      $.ajax({ 
       type: "POST", 
       url: "data_handler.php", 
       data: dataString,     
       cache: true, 
       success: function(response) { 
        alert(response); 
        $(elem).html(response); 
       } 
      }); 
     }); 

需要定義一個新的變量,以保護this在jQuery已經爲您完成。

4

由於您在回調中使用異步函數,因此回調中的this不是來自相同的上下文。您需要將this保存在回調中使用的變量中。

嘗試這樣的:

setInterval(function() { 
      $(".time").each(function(index) { 
       var sendTime= $(this).attr("data-time"); 
       dataString = "sendtime="+sendTime+"&q=convertTime"; 
       var self = this; 
       $.ajax({ 
        type: "POST", 
        url: "data_handler.php", 
        data: dataString,     
        cache: true, 
        success: function(response) { 
         alert(response); 
         $(self).html(response); 
         //alert(response); 
        } 
       }); 
      }); 
     }, 5000); 
+0

gr8 ..solved ... thankx – 2013-02-13 10:52:29

+0

這增加了無用的代碼:jQuery已經聲明瞭一個變量......查看我的答案。 – 2013-02-13 10:54:00

1

我覺得$(這)是斷章取義。嘗試:

setInterval(function() { 
      $(".time").each(function(index) { 
       var $this = $(this); 
       var sendTime= $(this).attr("data-time"); 
       dataString = "sendtime="+sendTime+"&q=convertTime"; 
       $.ajax({ 
        type: "POST", 
        url: "data_handler.php", 
        data: dataString,     
        cache: true, 
        success: function(response) { 
         alert(response); 
         $this.html(response); 
         //alert(response); 
        } 
       }); 
      }); 
}, 5000); 
1
setInterval(function() { 
     $(".time").each(function(index) { 
      var sendTime= $(this).attr("data-time"); 
      var _thisvariable = $(this); 
      dataString = "sendtime="+sendTime+"&q=convertTime"; 
      $.ajax({ 
       type: "POST", 
       url: "data_handler.php", 
       data: dataString,     
       cache: true, 
       success: function(response) { 
        alert(response); 
        _thisvariable.html(response); 
        //alert(response); 
       } 
      }); 
     }); 
    }, 5000);