2015-10-09 23 views
0

我有一個AJAX獲取新的HTML並替換response.html中的表單數據。替換前我需要更改一個屬性。如何更改AJAX響應HTML,然後在jQuery中繼續替換?

這裏是我的嘗試,但我不能得到正確的事:

success: function(response, status, xhr) { 
    if (response.data.html !== null) { 
     var newdata = response.data.html; 
     if(firstload == 1){ 
      var newhtml = $(newdata).find('.trigger').attr('data-firstload',0); 
      // now I need to replace newdata 
     } 
     $(container).html(newdata); 
    } 
} 
+0

我想應答已經是html字符串了。 JQuery適用於DOM元素,而不是HTML字符串。我想,你需要正則表達式或者影子技術來做你想做的事情。 – mondjunge

+1

'$(container).empty()。append($(newdata));' – lshettyl

回答

1

您可以創建包含新的HTML jQuery對象,對其進行更新,然後追加它。試試這個:

success: function(response, status, xhr) { 
    if (response.data.html !== null) { 
     var $newHtml = $(response.data.html); 

     if (firstload == 1) 
      $newHtml.find('.trigger').attr('data-firstload', 0); 

     $(container).empty().append($newHtml); 
    } 
},