2016-11-21 105 views
0

我陷入了一个令人尴尬的简单问题。我非常了解ASP.NET,但我没有那么好。我的主页上有一个精彩的倒计时钟,它使用各种JavaScript文件。我找到了存储结束日期的文件,它是一个静态值。将字符串作为变量传递到JavaScript函数不起作用

$(".count-down").ccountdown(2016,12,25,'10:00'); 

我想读的是从我的数据库填充,从而使他人在CMS可以设置一个管理面板中值hiddenfield的日期。我编码如下:

var countdowndate = document.getElementById('hdnCountdownDate').value; 
//alert(countdowndate); 
$(".count-down").ccountdown(countdowndate); 

(注;所述.ccountdown是不是一个错字)

的ASP.NET字段;

<form runat="server"> 
    <asp:HiddenField ID="hdnCountdownDate" runat="server" /> 
</form> 

主页的代码隐藏设置隐藏字段值;

hdnCountdownDate.Value = "2016,12,25,'10:00'" 

我把警报的变量的声明和设置后,它会给出确切的相同的值静态的,但是当我换变量与静态文本的功能,它仅适用。有人能帮助我吗?

呈现的HTML是;

<input type="hidden" name="ctl00$hdnCountdownDate" id="hdnCountdownDate" value="2016,12,25,'10:00'"> 

我试着将countdowndate变量拆分成它的变量变量,但是sitll不起作用;

var countdowndate = document.getElementById('hdnCountdownDate').value; 
var resarray = countdowndate.split(","); 
$(".count-down").ccountdown(resarray[0],resarray[1],resarray[2],resarray[3]); 

JS函数是;

(function($) { 
$.fn.ccountdown = function(_yr, _m, _d, _t) { 
    var $this = this; 
    var _montharray = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec") 
    var _today = new Date(); 
    // calling function first time so that it wll setup remaining time 
    var _changeTime = function() { 
     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(); 
     _todaysec = "0" + _todaysec; 
     _todaysec = _todaysec.substr(_todaysec.length - 2); 
     var _todaystring = _montharray[_todaym] + " " + _todayd + ", " + _todayy + " " + _todayh + ":" + _todaymin + ":" + _todaysec; 
     var _futurestring = _montharray[_m - 1] + " " + _d + ", " + _yr + " " + _t; 
     /* calculation of remaining days, hrs, min, and secs */ 
     _dd = Date.parse(_futurestring) - Date.parse(_todaystring); 
     _dday = Math.floor(_dd/(60 * 60 * 1000 * 24) * 1); 
     _dhour = Math.floor((_dd % (60 * 60 * 1000 * 24))/(60 * 60 * 1000) * 1); 
     _dmin = Math.floor(((_dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000))/(60 * 1000) * 1); 
     _dsec = Math.floor((((_dd % (60 * 60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) % (60 * 1000))/1000 * 1); 
     var el = $($this); 
     var $ss = el.find(".second"), $mm = el.find(".minute"), $hh = el.find(".hour"), $dd = el.find(".days"); 
     $ss.val(_dsec).trigger("change"); 
     $mm.val(_dmin).trigger("change"); 
     $hh.val(_dhour).trigger("change"); 
     $dd.val(_dday).trigger("change"); 
}; 
    _changeTime(); 
    setInterval(_changeTime, 1000); 
}; 

})(jQuery); 
+1

你确定你了解ASP.NET吗?如果你这样做,你会知道'hdnCountdownDate'不会是你隐藏字段的实际HTML ID。 –

+0

是的,我知道,但即使我将其更改为var,该警报仍会在隐藏字段中显示正确的值。countdowndate = document.getElementById('<%= hdnCountdownDate.ClientID%>')。它根本不起作用 –

+0

@AlastairW请使用parseInt像 –

回答

0

您需要将4个参数传递给.ccountdown():年,月,日和时间字符串。

在您的代码中,您只传递一个参数,该参数是从输入中获取的字符串。

+0

感谢您的回复,我已经包含声明变量的代码隐藏部分,肯定有4个变量传递,3个整数,时间是一个字符串。 –

+0

我保证你肯定会在这里传递一个参数:'$(“。count-down”)。ccountdown(countdowndate);',不是4. –

+0

我明白你的意思了。被传递的变量是一个变量,即使它是一个逗号分隔的字符串,其中包含4个变量。我已经将它们拆分为一个数组,然后传递每个数组,但这也不起作用。我做对了吗?请参阅上面的编辑 –

相关问题