php 使用ajax jquery刷新页面而不使用重新加载功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19645724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:49:28  来源:igfitidea点击:

refresh page with ajax jquery without use of reload function

javascriptphpjqueryhtmlajax

提问by Ronak Patel

I am performing delete records by using jquery ajax in php. I want to refresh that content without the use of location.reload() function. I tried this,

我正在通过在 php 中使用 jquery ajax 执行删除记录。我想在不使用 location.reload() 函数的情况下刷新该内容。我试过这个,

$("#divSettings").html(this);

but, it's not working. What's the correct logic to get updated content in div. Thanks.

但是,它不起作用。在 div 中获取更新内容的正确逻辑是什么。谢谢。

Code:

代码:

function deletePoll(postId){
    $.ajax({
        type: "POST",
        url: "../internal_request/ir_display_polls.php",
        data: {
          postId: postId
        },
        success: function(result) {
            location.reload();
            //$("#divSettings").html(this);
        }
     });
}

回答by kelunik

You're almost there:

您快到了:

function deletePoll(postId){
    $.ajax({
        type: "POST",
        url: "../internal_request/ir_display_polls.php",
        data: {
          postId: postId
        },
        success: function(result) {
            $("#divSettings").html(result); // <-- result must be your html returned from ajax response
        }
     });
}

回答by Paul Gleeson

You just needed to set the result into your '#divSettings' element using the .html() function :

您只需要使用 .html() 函数将结果设置到您的“#divSettings”元素中:

 $('#divSettings').html(result);

So a full example would look like :

所以一个完整的例子看起来像:

function deletePoll(postId){
    $.ajax({
        type: "POST",
        url: "../internal_request/ir_display_polls.php",
        data: {
          postId: postId
        },
        success: function(result) {
            //Sets your content into your div
            $('#divSettings').html(result);            
        }
     });
}

回答by Harsh

I believe you have to clear the section first and then you can attach the HTML again. Like

我相信您必须先清除该部分,然后才能再次附加 HTML。喜欢

function deletePoll(postId){
    $.ajax({
        type: "POST",
        url: "../internal_request/ir_display_polls.php",
        data: {
          postId: postId
        },
        success: function(result) {
            //Sets your content into your div
            $('#divSettings').html("");
            $('#divSettings').html(result);            
        }
     });
}

I believe this way you won't see the old content.

我相信这样你就不会看到旧的内容。