简体   繁体   English

jQuery刷新特定的div

[英]jQuery refreshing specific div

So I have a sortable li list that has a series of pages displayed. 因此,我有一个可排序的li列表,其中显示了一系列页面。

When I sort them, my jQuery code calls a php page that updates the order of my pages in the database. 当我对它们进行排序时,我的jQuery代码调用一个php页面,该页面更新数据库中页面的顺序。

On the same page as the list, it displays the pages in the order they are in but I want to be able to auto update this order right after my database is updated without actually refreshing the page. 在与列表相同的页面上,它按页面顺序显示页面,但是我希望能够在数据库更新后立即自动更新此顺序,而无需实际刷新页面。

So basically, secretly "refresh" the pageContainer div. 因此,基本上,秘密地“刷新” pageContainer div。

What is the best way to do this? 做这个的最好方式是什么?

<div id="pageContainer">
   <!-- include() a series of pages in the right order from database -->
</div>

 $("#pageContainer").sortable({
    stop : function(event, ui){
        var postData = $(this).sortable('serialize');
        var url = "saveOrder.php";
        $.ajax({
            type: "POST",
            url: url,
            data: postData,
            success: function(data) { $('#pageContainer').html(data);  }
        });
    }
});

EDIT So a simple $('#pageContainer').html(data); 编辑所以一个简单的$('#pageContainer').html(data); did the trick but my $('form').on('input propertychange change') stopped working. 做到了,但是我的$('form').on('input propertychange change')停止工作了。 The fix is on this page if anyone wants to look. 如果有人要查看此页面上的修复程序。

Create your sorted content on the php page just add that result to proper div. 在php页面上创建排序的内容,只需将结果添加到适当的div中即可。

so basically.. 所以基本上..

$("#pageContainer").sortable({
    stop : function(event, ui){
        var postData = $(this).sortable('serialize');
        var url = "saveOrder.php";
        $.ajax({
            url: url,
            data: postData,
            success: function(data) {  
            jQuery("#pageContainer").html(data);}
        });
    }
});

If you echo out the HTML you want in your saveOrder.php page, then the rest is simple: 如果您在saveOrder.php页面中回显所需的HTML,则其余的操作很简单:

$.ajax({
    type: "POST",
    url: url,
    data: postData,
    contentType: 'html',
    success: function(data) { 
        $('#pageContainer').html(data);
    }
});

Depending on your version of jQuery (v1.5+), you may wish to take advantage of the jQuery Promise that $.ajax returns. 根据您的jQuery版本(v1.5 +),您可能希望利用$.ajax返回的jQuery Promise Your code would change to: 您的代码将更改为:

$.ajax({
    type: "POST",
    url: url,
    data: postData,
    contentType: 'html'
}).done(function(data) {
    $('#pageContainer').html(data);
});

This allows you to chain callbacks and keeps your setup separate from your callback functions. 这使您可以链接回调,并使设置与回调函数分开。 For example, you could do this: 例如,您可以这样做:

...
}).done(function(data) {
    $('#pageContainer').html(data);
}).fail(function() {
    alert('failed :(');
}).always(function() {
    alert('done!');
});

you would have your ajax call return html like 您将让您的ajax呼叫返回html之类的

dataType: 'html'

Then in your success function you would do 然后,在您的成功职能中,您将

$('#pageContainer').html(data);

用“ delegate”代替“ on”怎么样?

$('#pageContainer').delegate('form', 'input propertychange change', function(){...;})

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

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