简体   繁体   中英

How to refresh or reload a div's content using Ajax

This is somehow. i normally use jquery's load to load content from page A into page B , so loading a div's content into itself in order to just refresh it doesn't make much sense. What's the solution for this?

$('#A').load('pageX.php #A');

Please note that both #As are on pageX making it the same #A

This somehow interferes with the JavaScript in a bad way after that "load" i don't know why.

So these is simply to refresh a div.

An id must to unique in html document otherwise you'll end up having expected results for JavaScript.

In your case you need to remove duplicate id's

Something like this

$('#A').load('pageX.php #B');

or this will work:

$('#B').load('pageX.php #A');

Because of people coming to this question, if I were to do this again now, I would do it like this.

HTML

<html>
<body>
    <div class="divToRefresh"></div>
</body>
</html>

JavaScript(JQuery)

  <script>
$(document).ready(function(e) {
    $.refreshDiv = function(arg){
        var d = {someVariable_you_can_send:arg}
        $.ajax({
            url:'url_to_div_content',
            data:d,
            type:"POST"}).done(function(result){
                $('.divToRefresh').html(result);    
            });     
    };
    //////////call this for the first time//////////
    $.refreshDiv('some value');
    //////////////////////////// and then refresh it after a certain interval if you need to
    window.setInterval(function(){
        $.refreshDiv('some value');
    }, 1000*60*60*60);

});
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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