我陷入了一個令人尷尬的簡單問題。我非常瞭解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);
你確定你瞭解ASP.NET嗎?如果你這樣做,你會知道'hdnCountdownDate'不會是你隱藏字段的實際HTML ID。 –
是的,我知道,但即使我將其更改爲var,該警報仍會在隱藏字段中顯示正確的值。countdowndate = document.getElementById('<%= hdnCountdownDate.ClientID%>')。它根本不起作用 –
@AlastairW請使用parseInt像 –