【问题标题】:Jquery Datepicker says object doesn't support property or method 'datepicker' within Jquery calls?Jquery Datepicker 说对象不支持 Jquery 调用中的属性或方法“datepicker”?
【发布时间】:2015-07-06 15:10:01
【问题描述】:

我正在尝试使用 jquery datepicker,而我的代码只是这样的:

$('#startDate').datepicker({
        showButtonPanel: true,
        dateFormat: 'MM yy'
});

它工作得很好,但是当我尝试在我的调用中再次使用 $(this) 甚至只是 $('startDate') 时,它会引发“对象不支持属性或方法'datepicker'”错误。 (就像下面的代码)

$('#startDate').datepicker({
        showButtonPanel: true,
        dateFormat: 'MM yy',
        $(this).datepicker('setDate', new Date(2015, 1, 1));
        // Also throws an error when I replace $(this) with $('startDate')
})

我什至只是将它包装在 $(document).ready() 中并仍然使用简单的工作版本...

$(document).ready(function() {
        $('#startDate').datepicker({
                showButtonPanel: true,
                dateFormat: 'MM yy'
        });
});

知道是什么导致了这个错误吗?

编辑

这是我正在尝试做的更完整的示例...

基本上,我希望能够根据变量 timeBucket 的值将日期选择器切换为月份选择器和日期选择器...

问题是,每当我在原始 $('#startDate').datepicker 中使用 $('#startDate').datepicker 时,它都会引发错误...

$('#startDate').datepicker({ changeMonth: true, changeYear: true, showButtonPanel: true, dateFormat: timeBucket == 2 ? 'MM yy' : "mm/dd/yy", onSelect: function (dateText, inst) { if (timeBucket == 2) { var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val(); var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val(); $('startDate').datepicker('setDate', new Date(year, month, 1)); } } }).focus(function () { var thisCalendar = $(this); if (timeBucket == 1) { $('.ui-datepicker-calendar').detach(); $('.ui-datepicker-calendar').css("display", "none"); } else { $('.ui-datepicker-calendar').css("display", ""); } });

【问题讨论】:

  • 你能创建一个 jsFiddle.net 的问题示例吗?
  • 我相信这是因为日期选择器仍在构建中。试试 defaultDate 属性http://api.jqueryui.com/datepicker/#option-defaultDate。或者,您可以添加以下行 $('#startDate').datepicker('setDate', new Date(2015, 1, 1));
  • // Also throws an error when I replace $(this) with $('startDate') 应该是$('#startDate'); 吗?不是完整的问题,只是我注意到的一些事情。
  • 应该是$('#startDate')
  • 这里的最终目标是什么?您在尝试执行 X 时遇到问题(您已经描述过),但是 X 是什么?如果您告诉我们,我们可以告诉您您是否接近正确的轨道。

标签: javascript jquery jquery-ui datepicker


【解决方案1】:

你在这里:

$("#StartDate").datepicker({
  dateFormat: 'dd/mm/yy',
  beforeShow: function() {
    // Everytime datepicker open, this function will run
    //$(this).datepicker('setDate', new Date(2015, 1, 1));
  }
});
// Set datetime here once when page load.
$('#StartDate').datepicker('setDate', new Date(2015, 1, 1));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" />Start:
<input type="text" id="StartDate" />

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-24
    • 1970-01-01
    • 2013-03-23
    • 2018-01-11
    • 2019-03-29
    • 2015-11-21
    • 2011-02-02
    • 1970-01-01
    相关资源
    最近更新 更多