【问题标题】:How to set the default month and the year of jquery monthpickerjquery monthpicker如何设置默认月份和年份
【发布时间】:2016-11-08 15:23:52
【问题描述】:

我需要在页面加载时为 jquery 月份选择器设置默认月份和年份。

下面是我的代码。目前加载时月份选择器文本框为空。

  $(document).ready(function() {      
    $('#monthPicker').datepicker( {
changeMonth: true,
changeYear: true,
showButtonPanel: true,
showButtonPanel: true,
dateFormat: 'MM yy',
onClose: function(dateText, inst) { 
    var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
    var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();

    $(this).datepicker('setDate', new Date(year, month, 1));
}
});
}); 

另外,我需要从日历中删除日期,我的意思是我只需要显示年份和月份选择选项。

【问题讨论】:

标签: javascript jquery jquery-ui-datepicker


【解决方案1】:

如果要在页面加载时显示默认值,只需在初始化日期选择器后设置日期即可:

$(function() {
  $('#monthPicker').datepicker( {
    // ...
  });

  var default_date = new Date(2023, 10, 1);
  $("#monthPicker").datepicker("setDate", default_date);
});

只需将任何日期设置为default_date。请记住,在 javascripts 中,月份从 0 开始(0 表示一月,11 表示十二月)。

【讨论】:

  • 我需要将它设置为当前日期。
  • 你能解释一下当前日期对你意味着什么吗?
  • 今天的日期,现在是固定的。我现在需要的是从日历中删除日期,只保留月份和年份的选择。而且,我还想实现在第二次单击日历时停留在最后选择的月份、年份。
猜你喜欢
  • 1970-01-01
  • 2015-09-07
  • 2012-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多