How to set the default month and the year of jquery monthpicker

[亡魂溺海] 提交于 2019-12-23 04:09:21

问题


I need to set the default month and the year for the jquery monthpicker when page loads.

Below is my code. Currently monthpicker text box is empty when loads.

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

And also, I need to remove the dates from the calendar, I mean I just only need to display the year and month selection options.


回答1:


If you want to show a default value when the page loads, just set the date after initializing the datepicker:

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

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

Just set whatever date to default_date. Rembemer that, in javascripts, months start on 0 (0 means january, and 11 means december).



来源:https://stackoverflow.com/questions/40490589/how-to-set-the-default-month-and-the-year-of-jquery-monthpicker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!