简体   繁体   English

在Ajax成功调用上重新加载页面后追加DOM元素

[英]Appending DOM elements after page reload on ajax success call

        $.ajax({
        type        : 'POST',
        url         : requesturl,
        data        : data,
        dataType    : 'json',
        success     : function(data) {
            location.reload();
            $('#status').append(
                                // append something here!!
            );
            $('#loader').hide();                
        }
    });

I'm having some issues forcing the page to reload first and then appending my elements. 我遇到了一些问题,迫使页面首先重新加载,然后附加我的元素。 I need this functionality to occur since the page will initialize again on reload and wipe out the elements I'm trying to append. 我需要此功能,因为页面将在重新加载时再次初始化并清除我尝试添加的元素。

Once you call location.reload() it's too late. 调用location.reload() ,为时已晚。 The scripts won't continue to run after the page has reloaded. 重新加载页面后,脚本将不会继续运行。

It sounds like you may need to rethink your general approach here. 听起来您可能需要在这里重新考虑一般方法。 A common approach would be to not use AJAX at all (since you plan to reload the page anyway), or have the server render the page in the desired new state, including whatever you were going to append to it. 一种常见的方法是根本不使用AJAX(因为无论如何您都打算重新加载页面),或者让服务器将页面呈现为所需的新状态,包括要添加到页面的任何内容。

you should not use location.reload() here Instead use $('.yoursubClass').remove() and then call append method. 您不应在此处使用location.reload(),而应使用$('。yoursubClass')。remove(),然后调用append方法。

 $.ajax({
        type        : 'POST',
        url         : requesturl,
        data        : data,
        dataType    : 'json',
        success     : function(data) {
            $('#status').find('yourRemovableElements').remove(); //modify yourRemovableElements accordingly 
            $('#status').append(
                                // append something here!!
            );
            $('#loader').hide();                
        }
    });

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

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