我正在使用Jquery的datepicker插件(工作正常)。然后,我需要从选择的日期中提取“星期几”。

使用foo.substring(0,3)分配datepicker('getDate')的前三个字符时,得到:TypeError foo.substr is not a function

$(function () {
    $("#textDatepicker").datepicker();
});

function selectedDay() {
    var foo = $("#textDatepicker").datepicker('getDate');

    //IF USED.... alert(foo);
    //retuens (for example).....
    //"Thu Jul 18 00:00:00 GMT-0400 (Eastern Standard Time)"

    var weekday = foo.substr(0, 3)
    document.getElementById("dayofweek").innerHTML = "The day of the week selected is: " + weekday;
}

<head>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script src="https://jquery-blog-js.googlecode.com/files/SetCase.js" type="text/javascript"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css"/>
</head>
<body>
Select Date:&nbsp;<input type="text" id="textDatepicker" onchange="selectedDay();">
<br><br>
<span id="dayofweek">Selected day of week replaces this</span>
</body>


我还粘贴了:jsfiddle

任何帮助将不胜感激..在此先感谢...

最佳答案

var foo = $("#textDatepicker").datepicker('getDate');


返回Date对象,而不是字符串,并且没有方法substr()

FIDDLE

您可以通过删除难看的内联事件处理程序并执行以下操作来解决该问题:

$("#textDatepicker").datepicker({
    onSelect: function() {
        var date = $(this).datepicker('getDate');
        var day  = $.datepicker.formatDate('DD', date);
        $('#dayofweek').html(day);
    }
});


FIDDLE

关于javascript - foo.substr给出“不是函数”错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17735717/

10-13 00:12