请查看以下javascript代码:
// Default calendar namespaces
var dateFormat = "<span class='day'>d</span> <span class='month'>M</span> <span class='year'>yy</span>",
dateArrival = '#dateArrival input',
dateDeparture = '#dateDeparture input',
dateArrivalVal = '#dateArrival .date-value',
dateDepartureVal = '#dateDeparture .date-value';
// Show arrival calendar
$(dateArrival).datepicker({
minDate: 'D',
dateFormat: dateFormat,
// get value on selected date for departure
onSelect: function (txt, inst) {
$(dateDepartureVal).html(txt);
$(dateArrivalVal).html($(dateArrival).val());
},
onClose: function (selectedDate) {
$(dateDeparture).datepicker("option", "minDate", selectedDate);
}
});
// Show departure calendar
$(dateDeparture).datepicker({
minDate: 'D+1',
dateFormat: dateFormat,
// get value on selected date for return
onSelect: function (txt, inst) {
$(dateDepartureVal).html(txt);
$(dateDepartureVal).html($(dateDeparture).val());
}
});
// set current date
$('.datepicker').datepicker('setDate', 'today');
// get current value from departure
$(dateArrivalVal).html($(dateArrival).val());
// get current value from return
$(dateDepartureVal).html($(dateDeparture).val());
正如您所看到的,它是一个预订小部件,可以在办理登机手续时显示当天,也可以在退房时显示当天。但是,当客户选择另一天办理登机手续时,退房时间设置为同一天。我想要做的是,如果选择dateArrival作为6月7日,则dateDeparture会自动显示6月8日(即dateArrival + 1天),而不是显示相同的日期(6月7日)。怎么能这样做?
非常感谢!
答案 0 :(得分:0)
你可以:
但这里的诀窍是,datepicker中没有任何方法可以帮助您将+1天设置为任意日期,因此您必须直接操作您的格式化字符串。 我添加了3行来产生+1天的字符串,第四个使用datepicker方法来设置dateDeparture。经过测试,检查它是否适合您
// Show arrival calendar
$(dateArrival).datepicker({
minDate: 'D',
dateFormat: dateFormat,
// get value on selected date for departure
onSelect: function (txt, inst) {
$(dateDepartureVal).html(txt);
$(dateArrivalVal).html($(dateArrival).val());
// ADDED THESE FIVE LINES
var d = new Date((+inst.currentMonth+1) + '/' + inst.currentDay + '/' + inst.currentYear)
var dpo = new Date(d.getTime() + 24*60*60*1000)
var str = "<span class=day>"+dpo.getDate()+"</span> <span class=month>"+(dpo.toDateString().split(' ')[1])+"</span> <span class=year>"+dpo.getFullYear()+"</span>"
$(dateDeparture).datepicker('setDate', str);
$(dateDepartureVal).html(str);
}
// REMOVED USELESS ONCLOSE, SINCE UI IS CLOSED ON SELECT
});