【问题标题】:use one countdown script for multiple countdowns使用一个倒计时脚本进行多个倒计时
【发布时间】:2012-07-26 08:24:49
【问题描述】:

我想使用这样的倒计时脚本

     var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")

function countdown(yr,m,d,idCountdown){
    var theyear=yr;
    var themonth=m;
    var theday=d;
    var today=new Date();
    var todayy=today.getYear();
    if (todayy < 1000)
        todayy+=1900
    var todaym=today.getMonth();
    var todayd=today.getDate();
    var todayh=today.getHours();
    var todaymin=today.getMinutes();
    var todaysec=today.getSeconds();
    var todaystring=montharray[todaym]+" "+todayd+", "+todayy+" "+todayh+":"+todaymin+":"+todaysec;
    var futurestring=montharray[m-1]+" "+d+", "+yr;
    var dd=Date.parse(futurestring)-Date.parse(todaystring);
    var dday=Math.floor(dd/(60*60*1000*24)*1);
    var dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1);
    var dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1);
    var dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1);
    var dag = "dagen";

    if(dday==0&&dhour==0&&dmin==0&&dsec==1){
        document.getElementById(idCountdown).value=current;
        return;
    }
    else if(dday<=1) {
        dag = 'dag';
    }
    document.getElementById(idCountdown).value=dday+ ' ' + dag + ' ' +dhour+" uur "+dmin+" min "+dsec+" sec";
    setTimeout(function(){countdown(theyear,themonth,theday,idCountdown);}, 1000);
}

在我的 html 页面中,我将有一个这样的文本字段

 <input name="aktie1" type="text" class="countdownTekst" size="27" readonly="readonly">
 <input name="aktie2" type="text" class="countdownTekst" size="27" readonly="readonly">

那我就用这个来使用函数

countdown(2012,7,30, 'aktie1')

最后一个是文本字段的 ID。但是当我这样做时,我会得到一个错误

 document.forms.count.idCountdown.value is undefined

如何在该函数中获取该文本字段的 ID?

【问题讨论】:

    标签: javascript forms countdown


    【解决方案1】:

    您需要像这样访问输入:document.forms.count[idCountdown].value

    编辑:这个小提琴正在工作 http://jsfiddle.net/Fy2FM/4/ 。更大的问题是你没有用var yourvar 声明变量,这使得变量成为全局对象的一部分。因此,您在两个倒计时中看到的总是相同的,因为实际上您访问的是相同的变量。

    【讨论】:

    • 谢谢。当我这样做时,表单字段会加载天数等,但它不再倒计时,我在 FB 中收到此错误:TypeError: document.forms.count[idCountdown] is undefined
    • @RickWeller 也许问题就在这里:setTimeout("countdown(theyear,themonth,theday)", 1000)。试试这个:setTimeout(function(){countdown(theyear,themonth,theday,idCountdown);}, 1000);
    • 这似乎是问题所在。只有当我这样做时它才会加载所有倒计时但是当它开始计数时它只会使用 aktie2 日期:(
    • @RickWeller 你能用 jsfiddle 显示所有代码吗?最好尝试找出问题所在。只是为了确定,您希望两个输入元素都倒计时,不是吗?
    • 小提琴对我有用。如果您不通过 id 属性更改 de name 属性,它也适用于 document.forms.count[idCountdown].value。
    【解决方案2】:

    在此脚本中添加特定时间例如:3:44

    <script language="javascript">countdown(2016,6,20,15:44 'aktie1')</script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-06
      • 1970-01-01
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-24
      相关资源
      最近更新 更多