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.

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

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)