duanfeigui6655 2016-02-05 07:40
浏览 86
已采纳

动态创建的html中的jquery倒计时

i need a little help using the jquery countdown keith wood - jquery countdown plugin

I am creating several countdowns by retrieving data from mysql database (php ajax call) and putting it into a div:

in php (getdetails.php -> gets $mid and $time from mysql-database):

    $mrow="TimeCounter inserted here:"
    $mrow.="<div id=\"RowDiv\"><div id=\"timecount".$mid."\"><script> $('#timecount".$mid."').countdown({until: ".$time."}); </script></div></div>";
    $mrow.="TimeCounter ends here";

in JS i set the innerHTML with the data i got:

    var url="getDetails.php";
    var what="getTimeData"; 
       console.log("call getTimeData");
    var p1 = $.post(url,{what:what,selId:selValue,conName:"GId"});
        p1.done(function(data){
        console.log("Data -> : "+ data);
        document.getElementById("CounterDiv").innerHTML=data
      });

console.log(data) shows me the set html:

    <div id="RowDiv" ><div id="timecount2"><script> $('#timecount2').countdown({until: 1454713200}); </script></div></div>

I get no errors but i dont see the counter... I do see the surrounding TimeCounter inserted here: TimeCounter ends here on the page. I suppose it is a client / server side issue. Maybe i need to call the function again after setting the innerHTML with the data. But i dont know how.

How can i solve this? Any ideas?

  • 写回答

2条回答 默认 最新

  • duan051347 2016-02-05 09:42
    关注

    Instead of adding an inline script within your HTML element, you can initiate the counter within your callback/success function of jQuery.post(). In order to do this, you will have to change your PHP and JS like below:

    PHP

    $mrow="TimeCounter inserted here:"
    $mrow.="<div id=\"RowDiv\"><div id=\"timecount" . $mid . "\" data-time=\"" . $time . "\"></div></div>";
    $mrow.="TimeCounter ends here";
    

    JS

    var url = "getDetails.php",
        what = "getTimeData";
    
    $.post(url, {what:what,selId:selValue,conName:"GId"}, function(data){
        $('#CounterDiv').html(data).find('[id^="timecount"]').countdown({until: $(this).data('time')*1});
    });
    

    UPDATE: I don't know the plugin, but the scope of this might get changed when .countdown() is called. In such a case, you can use .each() function of jQuery to pass the element. Here is how you do that:

    $.post(url, {what:what,selId:selValue,conName:"GId"}, function(data){
        var $counters = $('#CounterDiv').html(data).find('[id^="timecount"]'),
            $el,t;
        $counters.each(function(index,element){
           $el = $(element);
           t = $el.data('time')*1;
           $el.countdown({until: t});
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。