2010-04-16 82 views
1

我有jQuery的AJAX中正在使用的一些內容傳遞到數據庫中,但我的問題無關,與DB ..爲什麼我的內容被覆蓋而不是在jQuery/Ajax中被替換?

我有一個ID輸入字段:

#clientscontainer
。當我在該容器中單擊「保存」時,它會自動正確刷新容器...
 $('#clientscontainer').html(html);

問題是,有些輸入字段(例如說明和標題)在另一個div中有實例想要在點擊保存時刷新。另一個ID是:

 $('div#' + clientID')

當我做

$('div#' + clientID').html(html);
時,它刷新客戶端容器中的內容,而不僅僅是我想要更新的變量。

當我嘗試僅傳遞變量

$(blurb).html(html);
它更新blurb,但它只在div#clientID div中顯示該變量...而我只是想替換它。

下面是函數

 
...//variables// 
    dataToLoad = 'clientID=' + clientID + '&changeClient=yes' + '&project=' + 
descriptionSubTitle + '&campaign=' + descriptionTitle + '&label=' + 
descriptionLabel + '&descriptionedit=' + description +  '&blurbedit=' + blurb; 

    $.ajax({ 
    type: 'post', 
    url: ('/clients/controller.php'), 
    datatype: 'html', 
    data: dataToLoad, 
    success: function(html){   
     dataToLoad = 'clientID=' + clientID + '&loadclient=yes&isCMS=' + editCMS; 
    $.ajax({ 
    type: 'post', 
    url: '/clients/controller.php', 
    datatype: 'html', 
    data: dataToLoad, 
    async: false, 
    success: function(html){ 

     //$('#clientscontainer').focus(function() {reInitialize()}); 
     //$('#clientscontainer').ajaxComplete(function(){reInitialize()}); 

     $('#clientscontainer').html(html); 
     $('div#' + clientID).each(function(){ 

     $('#editbutton').click(function() {EditEverything()}); 


     } 
      , 
    error: function() { 
    alert('An error occured! 222'); 
    } 
    });}, 
     error: function() { 
     alert('An error occured! 394'); 
          } 
    }); 

任何建議的AJAX部分?

回答

0

你可以產生一個jquery對象與用於數據的DOM在回調

$(html) 

然後針對兩個輸入的主容器的外部,並使用.replaceWith()更換整個元件

$("div#").replaceWith($(html).find("div#")); 
$("clientID").replaceWith($(html).find("clientID")); 
傳遞迴
+0

感謝您的建議替換它,我給這個一杆 – 2010-04-16 21:05:30

0

這只是一個建議,但 $('div#' + clientID').html(html);可能會導致一些問題。

嘗試用

$('div#' + clientID').each(function(){ 
    $(this).html(variableOrArrayElement) 
});