【问题标题】:error when calling date.getMonths()调用 date.getMonths() 时出错
【发布时间】:2025-11-30 22:10:01
【问题描述】:

此页面提供了非常适合我正在处理的任务的示例代码

Datepicker for web page that can allow only specific dates to be clicked

但是,当我实现它时,我收到以下错误: TypeError: 表达式'date.getMonths' [undefined] 的结果不是函数。

我在 jquery ui 的官方网站上使用相同版本的 jquery (1.4.4) 和 jquery ui 进行测试 非常感谢任何帮助!

<!DOCTYPE html>
<html>
<head>
<base href="http://jqueryui.com/demos/datepicker/">
<title>TEST</title>
<link rel="stylesheet" href="../../themes/base/jquery.ui.all.css">
<script src="../../jquery-1.4.4.js"></script>
<script src="../../ui/jquery.ui.core.js"></script>
<script src="../../ui/jquery.ui.widget.js"></script>
<script src="../../ui/jquery.ui.datepicker.js"></script>
<link rel="stylesheet" href="../demos.css">

<script>
$(function() {

dates_allowed = {'2/26/2011': 1,'4/2/2011': 1};
$.datepicker.setDefaults({minDate: "+0"});      
$.datepicker.setDefaults({maxDate: (new Date(2011, 3, 2))});

    $('#Date').datepicker({

    // called for every date before it is displayed
    beforeShowDay: function(date) {

        var date_str = [
            date.getFullYear(),
            date.getMonths() + 1,
            date.getDay()
        ].join('-');

        if (dates_allowed[date_str]) {
            return [true, 'good_date', 'This date is selectable'];
        } else {
            return [false, 'bad_date', 'This date is NOT selectable'];
        }
    }
});
});
</script>
</head>
<body>
<input type="text" id="Date">
</body>
</html>

【问题讨论】:

    标签: jquery datepicker uidatepicker typeerror


    【解决方案1】:

    那是因为它是.getMonth,单数。

    【讨论】:

    • 就是这样,回想起来很明显!谢谢!
    【解决方案2】:

    很可能是错字。

    使用date.getMonth()

    【讨论】:

    • 就是这样,回想起来很明显!谢谢!