简体   繁体   English

刷新div ajax中的元素

[英]Refresh element in a div ajax

I have a small web application that manages some lessons and reports the statistics for them (completed and not completed). 我有一个小型的Web应用程序,它可以管理一些课程并报告它们的统计信息(已完成和未完成)。 When one clicks on the .finish button, I would like to refresh the statistics div of the lessons. 当单击.finish按钮时,我想刷新课程的统计信息div

Here is the php file: 这是php文件:

 <?php
    include_once('config_mysql.php');
    $data = new MysqlClass();
    $data->connect();
    $sql = "select * from Lessons";
    $result = mysql_query($sql);
    <div class="history">
    while($row = mysql_fetch_array($result)) {
     <div>
      <h2>name: <?php echo $row['name']; ?>
      <h2>completed: <?php echo $row['completed']; ?>
     </div>
    }
   </div>
    ?>

And the event handler attached to the .finish class: 并将事件处理程序附加到.finish类:

$(document).ready(function () {
    $('.finish').click(function(event){
        completeLesson();
         $.ajax({
                    type: "POST",
                    url: "statistic.php",
                    success: function(html){

                    }
        });  
    }
}

I did not include the completeLesson() function (which also makes an AJAX call) because it is not important. 我没有包括completeLesson()函数(该函数也进行AJAX调用),因为它并不重要。

I would like to refresh the statistics of lessons without refreshing the page. 我想刷新课程统计信息而不刷新页面。 I tried to refresh it with an AJAX call but it did not update the data. 我试图通过AJAX调用刷新它,但是它没有更新数据。 Thank you in advance for your answers. 预先感谢您的回答。

You have atleast 2 problems. 您至少有2个问题。

First, you need to do something with the variable html in your success callback. 首先,您需要在success回调中使用变量html进行操作。

Second, since completeLesson makes its own ajax call, you have to wait for that call to return before you put in your call to statistic.php. 其次,由于completeLesson进行了自己的ajax调用,因此您必须等待该调用返回,然后才能调用statistic.php。 if you don't wait, there's a chance that the second Ajax call will be completed before the first and it won't bring in the new data. 如果您不等待,那么第二个Ajax调用可能会在第一次调用之前完成,并且不会引入新数据。

One solution would be to pass completeLesson the second Ajax call as a callback function. 一种解决方案是将第二个Ajax调用传递给completeLesson作为回调函数。

function completeLesson ( /* your other arguments */, callback) {
    // this would be the Ajax call already in completeLesson
    $.ajax ( {
        // whatever arguments you use for the ajax call
        success: function (data) {
            // the code you had in here before...
            // after all that code add these 2 lines
            if (callback)
                callback();
        }
    });
}

And now, you change the event listener for finish to (assuming statistic.php is echo'd out between <div id="result"><?php include 'statistic.php'; ?></div> and also that you've added id="#hs" to the .history div): 现在,您将事件侦听器的结束状态更改为(假设statistic.php在<div id="result"><?php include 'statistic.php'; ?></div> ,已将id="#hs".history div):

$('.finish').click(function(event) {
    completeLesson(function() {
        $.ajax({
            type: "POST",
            url: "statistic.php",
            success: function(html) {
                $('#hs').remove();
                $('#result').prepend(html);
            }
        });
    });  
}

You may want to use $('#result').append(html) instead of prepend . 您可能要使用$('#result').append(html)而不是prepend

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

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