简体   繁体   English

jQuery - 在AJAX调用之后,旧元素仍然存在于DOM中? 怎么删除?

[英]jQuery - after AJAX Call, old elements still exist in DOM? How to remove?

I have a button to refresh the content using AJAX, calling refreshFeed(). 我有一个按钮来使用AJAX刷新内容,调用refreshFeed()。 I still need to manipulate the content loaded by AJAX so I appended .live() to some links in the AJAX-loaded content to initiate other AJAX calls, let's say "Comment". 我仍然需要操纵AJAX加载的内容,所以我将.live()附加到AJAX加载内容中的某些链接以启动其他AJAX调用,让我们说“评论”。 "Comment" button gather some values from the hidden input fields and post them to server. “注释”按钮从隐藏的输入字段中收集一些值并将它们发布到服务器。

It works fine if I don't click refresh. 如果我不点击刷新,它工作正常。 However, I found that after several refreshes, when I click the links that have .live('click', function(){}) bond (The "comment" button), it will send multiple POST requests to server. 但是,我发现经过几次刷新后,当我点击具有.live('click',function(){})bond(“注释”按钮)的链接时,它会向服务器发送多个POST请求。 I guess it's because the old DOM elements still exist, so I tried to remove all elements using .remove(), .empty() in refreshFeed(). 我想这是因为旧的DOM元素仍然存在,所以我尝试在refreshFeed()中使用.remove(),. empty()删除所有元素。 But the problem remains. 但问题仍然存在。

Here is the code: 这是代码:

$.ajaxSetup({
    cache: false
});
$(".submit-reply").live('click', function () {
    var mIDValue = $(this).parent().find("input.re-fb-msg-id").val();
    var accessValue = $(this).parent().find("input.re-fb-token").val();
    var commentValue = $(this).parent().find(".comment").val();
    var postReplyUrl = "fconfirm.jsp";
    var ajax_reply_load = "<img src='img/scanningsmall.gif' alt='loading...' />";
    $(this).parent().parent().find(".result-note").show();
    $(this).parent().parent().find(".result-note .out-container").html(ajax_reply_load).fadeIn(300).load(postReplyUrl, {
        mID: mIDValue,
        access: accessValue,
        comment: commentValue,
    });
    $(this).parent().hide();
});

function refreshFeed() {
    $.ajaxSetup({
        cache: false
    });
    $("#feeds div").remove();
    $(".submit-reply").remove();
    var loadingFeeds = "<div class='loading-feed'><img src='img/scanningsmall.gif' alt='loading...' /><p>Loading...</p></div>"
    var feedURL = "pbsc.htm"
    var feedParameter = "clientId=<%=clientId%>&getPostTweets=1&rescan=1";
    $("#feeds").html(loadingFeeds).load(feedURL, feedParameter);
}

I am new to Jquery and really don't know where the problem is. 我是Jquery的新手,真的不知道问题出在哪里。 Please help me! 请帮我! Thank you! 谢谢!

if your old elements have sent request those can be stopeed by using ajax request object. 如果您的旧元素已发送请求,则可以使用ajax请求对象来停止这些请求。 you can use ajax request object. 你可以使用ajax请求对象。

var request;// have global variable for request

//...........
    var request = $.ajax({
        type: 'GET',
        url: 'someurl',
        success: function(result){
         $(yourSelecter).html(result);
       }
    });




function refreshFeed() {
request.abort()// in refreshfeed function you can abort it

}

and if you have sevaral ajax requests you can use an array of ajax requests 如果你有sevaral ajax请求,你可以使用一组ajax请求

$.xhrPool = [];//global variable for array $ .xhrPool = []; //数组的全局变量

$.xhrPool.abortAll = function() {  
  _.each(this, function(jqXHR) {
    jqXHR.abort();
  });
};
$.ajaxSetup({
  beforeSend: function(jqXHR) {
    $.xhrPool.push(jqXHR);
  }
});

........EDIT......... ........编辑.........

I think your problem is with live function. 我认为你的问题在于实时功能。 I assume that your .submit-reply is in pbsc.htm so instead of using live function try this code. 我假设你的.submit-replypbsc.htm所以不要使用live函数试试这段代码。 This will 这将

function BindClick(){

 $(".submit-reply").Click(function () {
    var mIDValue = $(this).parent().find("input.re-fb-msg-id").val();
    var accessValue = $(this).parent().find("input.re-fb-token").val();
    var commentValue = $(this).parent().find(".comment").val();
    var postReplyUrl = "fconfirm.jsp";
    var ajax_reply_load = "<img src='img/scanningsmall.gif' alt='loading...' />";
    $(this).parent().parent().find(".result-note").show();
    $(this).parent().parent().find(".result-note .out-    container").html(ajax_reply_load).fadeIn(300).load(postReplyUrl, {
        mID: mIDValue,
        access: accessValue,
        comment: commentValue,
    });
    $(this).parent().hide();
 });

}

function refreshFeed() {
    $.ajaxSetup({
        cache: false
    });
    $("#feeds div").remove();
    $(".submit-reply").remove();
    var loadingFeeds = "<div class='loading-feed'><img src='img/scanningsmall.gif' alt='loading...' /><p>Loading...</p></div>"
    var feedURL = "pbsc.htm"
    var feedParameter = "clientId=<%=clientId%>&getPostTweets=1&rescan=1";
    $("#feeds").html(loadingFeeds).load(feedURL, feedParameter,BindClick);
}

In the first part of your code, try: 在代码的第一部分中,尝试:

$(".submit-reply").live('click', function(e) {
  e.preventDefault();
  // rest of your code here 
});

passing in "e" and calling e.preventDefault() will prevent any other HTML from running (if for example .submit-reply is set to submit a form). 传入“e”并调用e.preventDefault()将阻止任何其他HTML运行(如果例如.submit-reply设置为提交表单)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM