douwei1904 2016-09-15 23:42
浏览 63
已采纳

刷新div ajax中的元素

I have a small web application that manages some lessons and reports the statistics for them (completed and not completed). When one clicks on the .finish button, I would like to refresh the statistics div of the lessons.

Here is the php file:

 <?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:

$(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.

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. Thank you in advance for your answers.

  • 写回答

1条回答 默认 最新

  • dtt78245 2016-09-16 00:07
    关注

    You have atleast 2 problems.

    First, you need to do something with the variable html in your success callback.

    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. 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.

    One solution would be to pass completeLesson the second Ajax call as a callback function.

    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):

    $('.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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料