简体   繁体   中英

Passing a string as a variable into JavaScript function doesn't work

I'm stuck on an embarrassingly simple problem. I know ASP.NET really well but I don't JavaScript that well. I have a great countdown clock on my homepage that uses various JavaScript files. I found the file where the end date is stored and it is a static value.

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

I want to read the date from a hiddenfield that is populated from my database so that someone in the CMS can set the value in an admin panel. I coded the following;

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

(Note; the .ccountdown isn't a typo)

The ASP.NET field is;

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

The codebehind for the homepage sets the hiddenfield value;

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

I placed an alert after the declaration and setting of the variable and it gives the exact same value as the static one but it only works when I swap the variable in the function with static text.

The rendered HTML is;

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

I tried splitting the countdowndate variables into it's consistuent variables like so but sitll doesn't work;

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

The JS function is;

(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);

You need to pass 4 arguments to .ccountdown() : year, month, day and the time string.

In your code you pass only one argument which is a string taken from the input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM