如果#datepicker1的日期在#datepicker2的日期之后,如何修改此函数以将#datepicker2的值更改为#datepicker1的日期加一天?
因此,如果#datepicker1和#datepicker两个的默认日期分别为11/03/2011和11/04/2011,并且我将#datepicker1更改为11/25/2011,那么#datepicker2应该填充为11/26 / 2011。
但是,如果我将#datepicker1更改为,例如,10/28/2011#datepicker2保持不变。
Heres a jsfiddle - > http://jsfiddle.net/hJrzp/1/
$(function() {
var dates = $( "#datepicker1, #datepicker2" ).datepicker({
// minDate: new Date(),
// maxDate: '+2y',
defaultDate: "+1d",
showOtherMonths: true,
selectOtherMonths: true,
// changeMonth: true,
numberOfMonths: 1,
onSelect: function( selectedDate ) {
var option = this.id == "datepicker1" ? "minDate" : "maxDate",
instance = $( this ).data( "datepicker" ),
date = $.datepicker.parseDate(
instance.settings.dateFormat ||
$.datepicker._defaults.dateFormat,
selectedDate, instance.settings );
dates.not( this ).datepicker( "option", option, date );
}
});
});
答案 0 :(得分:1)
当datepicker是第一个时,你应该可以添加一天(即#datepicker1
)。这涉及一些日期算术。因此,onSelect
函数看起来像:
onSelect: function( selectedDate ) {
var option = this.id == "datepicker1" ? "minDate" : "maxDate",
instance = $( this ).data( "datepicker" ),
date = $.datepicker.parseDate(
instance.settings.dateFormat ||
$.datepicker._defaults.dateFormat,
selectedDate, instance.settings );
if (this.id == 'datepicker1') {
var oneDay = 1000*60*60*24;
var plusOneDay = new Date(date.getTime() + oneDay);
dates.not( this ).datepicker( "option", option, plusOneDay );
} else {
dates.not( this ).datepicker( "option", option, date );
}
}