【问题标题】:run javascript after loading data from ajax从ajax加载数据后运行javascript
【发布时间】:2015-06-29 19:08:59
【问题描述】:

index.php

<script type="text/javascript">
$( document ).ready(function() {
   $('[data-countdown]').each(function() {

      var $this = $(this), finalDate = $(this).data('countdown');

      $this.countdown(finalDate, function(event) {

         $this.html('Time Left : '+ event.strftime('%D days %H:%M:%S'))}).on('finish.countdown', function() {

            $this.html('This campaignn has expired!');

          });
     });
 });

</script>

这是用于显示倒数计时器的 javascript。 我正在使用 jquery.countdown.min.js 插件。

<div class="counter-inner"> <div id="example<?php echo $rec['cid'];?>" data-countdown="<?php echo date("m/d/Y h:i:s", strtotime($rec['Closing_Date']));?>"></div></div><p>

Closing_Date 和 cid 来自数据库。 当我访问它工作正常。

但是当我通过 ajax 将数据插入 index.php 时。它什么也没做。好像没有执行javascript代码。

我检查了萤火虫,发现日期来到 index.php 文件,但它没有显示倒计时。

请指教

Ajax

$(document).ready(function() {
    $('#more').click(function() {
        var get_last_post_display = $('.unique-class:last').attr('id');

        $('#more').html('<img src="ajax-loader.gif"');
        $.post('more_prj.php', 'last_id_post='+get_last_post_display, function(html) {
            if(html) {

                $('#main-div').append(html);
                $('#more').text('Load More Post'); //add text "Load More Post" to button again
            } else {
                $('#more').text('No more Post to load'); // when last record add text "No more posts to load" to button.
            }
        }, 'json');
    });
});

【问题讨论】:

  • 在您的 AJAX 回调中重新运行 .each()
  • 你能显示你的 AJAX 调用代码吗?
  • move $('[data-countdown]').each(function() { $('#main-div').append(html)后的代码块;
  • @droid 我将在 ajax 中更新上述代码。还有什么要改变的吗?
  • 成功了。你能把它粘贴在答案中吗?谢谢

标签: javascript jquery ajax jquery-countdown


【解决方案1】:

移动 $('[data-countdown]').each(function() 到 ajax js,像这样:

$(document).ready(function() {
  $('#more').click(function() {
    var get_last_post_display = $('.unique-class:last').attr('id');

    $('#more').html('<img src="ajax-loader.gif"');
    $.post('more_prj.php', 'last_id_post='+get_last_post_display, function(html) {
        if(html) {
            $('#main-div').append(html);
            $('#more').text('Load More Post'); //add text "Load More Post" to button again
            $('[data-countdown]').each(function() {
                var $this = $(this), finalDate = $(this).data('countdown');
                $this.countdown(finalDate, function(event) {
                $this.html('Time Left : '+ event.strftime('%D days %H:%M:%S'))}).on('finish.countdown', function() {
                    $this.html('This campaignn has expired!');
                });
            });
        } else {
            $('#more').text('No more Post to load'); // when last record add text "No more posts to load" to button.
        }
    }, 'json');
  });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 2015-12-09
    • 1970-01-01
    • 2018-02-04
    • 2015-10-16
    • 1970-01-01
    • 2015-07-27
    相关资源
    最近更新 更多