问题
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