为什么我的内容被覆盖而不是在jQuery / Ajax中被替换?

时间:2010-04-16 17:50:28

标签: jquery ajax

我在ajax中使用了jquery将一些内容传递到数据库中,但我的问题与db无关。

我在名为

#clientscontainer
的ID中输入了字段。当我点击该容器中的“保存”时,它会自动正确刷新容器...
 $('#clientscontainer').html(html);

问题是,其中一些输入字段(例如描述和标题)在另一个div中有实例,我想在保存点击时刷新。另一个ID是:

 $('div#' + clientID')

当我执行

$('div#' + clientID').html(html); 
时,它会刷新来自客户端容器的内容,而不仅仅是我想要更新的变量。

当我尝试仅传递变量

$(blurb).html(html); 
时,它会更新blurb,但它只在div#clientID div中显示该变量...而我只是想替换它。

这是函数的AJAX部分

...//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');
                          }
    });

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

您可以为回调中传回的数据生成带有DOM的jquery对象

$(html) 

然后将两个输入定位在主容器外部,并使用.replaceWith()替换整个元素

$("div#").replaceWith($(html).find("div#"));
$("clientID").replaceWith($(html).find("clientID"));

答案 1 :(得分:0)

这只是一个建议,但是 $('div#' + clientID').html(html);可能会导致一些问题。

尝试用

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