更新时间:2023-01-31 15:34:20
设置$('#toDate').datepicker()
时,$('#fromDate')
的值为空.
您应该在开始时设置默认的startDate
和endDate
变量,并添加 changeDate
事件监听器,用于您的日期选择器.
You should set default startDate
and endDate
variables at the begining and add changeDate
event listener to your datepickers.
例如:
// set default dates
var start = new Date();
// set end date to max one year period:
var end = new Date(new Date().setYear(start.getFullYear()+1));
$('#fromDate').datepicker({
startDate : start,
endDate : end
// update "toDate" defaults whenever "fromDate" changes
}).on('changeDate', function(){
// set the "toDate" start to not be later than "fromDate" ends:
$('#toDate').datepicker('setStartDate', new Date($(this).val()));
});
$('#toDate').datepicker({
startDate : start,
endDate : end
// update "fromDate" defaults whenever "toDate" changes
}).on('changeDate', function(){
// set the "fromDate" end to not be later than "toDate" starts:
$('#fromDate').datepicker('setEndDate', new Date($(this).val()));
});