jquery datepicker after setDate no longer able to

2019-08-02 09:22发布

问题:

I'm using the JQuery UI Datepicker to pick date. I set "changeMonth" and "changeYear" to true at the initialization.

$( "#from" ).datepicker({
  defaultDate: "+1w",
  changeMonth: true,
  changeYear: true,
  dateFormat: 'yy-mm-dd',
  onClose: function( selectedDate ) {
    $( "#to" ).datepicker( "option", "minDate", selectedDate );
  }
});
$( "#to" ).datepicker({
  defaultDate: "+1w",
  changeMonth: true,
  changeYear: true,
  dateFormat: 'yy-mm-dd',
  onClose: function( selectedDate ) {
    $( "#from" ).datepicker( "option", "maxDate", selectedDate );
  }
});

But I want the datepicker to be set by default, so I used setDate BEFORE THE SETTING of changeMonth and changeYear:

$(#from).datepicker('setDate', '10-10-2010');
$(#to).datepicker('setDate', '10-10-2011');

This was successful. However, in the dropdown calendar on the header I could no longer directly change month and year.

Does anyone know what's happened or know the solution to this? Or I should rather change another way to set Date?

Thanks in advance for your help!

回答1:

I tried the same script that you have used. It works for me.Check this http://jsfiddle.net/XE68u/

 $( "#from" ).datepicker({
  defaultDate: "+1w",
  changeMonth: true,
  changeYear: true,
  dateFormat: 'yy-mm-dd',
  onClose: function( selectedDate ) {
    $( "#to" ).datepicker( "option", "minDate", selectedDate );
  }
});
$( "#to" ).datepicker({
  defaultDate: "+1w",
  changeMonth: true,
  changeYear: true,
  dateFormat: 'yy-mm-dd',
  onClose: function( selectedDate ) {
    $( "#from" ).datepicker( "option", "maxDate", selectedDate );
  }
});

$("#from").datepicker('setDate', '10-10-10')
$("#to").datepicker('setDate', '11-10-10')