我正在使用http://www.daterangepicker.com/创建两个单独的日历。一个用于开始日期,一个用于结束日期。What I want to happen is when the first date is chosen the second one will automatically have the value 7 days ahead. 当前页面加载提前 7 天,但是当第一个日历更改时,我无法更改第二个日历。
更改时也需要更新隐藏的 html 输入字段
下面的代码:
<label for="datetimepickerin" class="date-input-holder__label"><i class="glyphicon glyphicon-calendar fa fa-calendar"></i></label>
<input type="text" class="form-control datetimepicker datetimepicker--checkin" name="datetimepickerin" id="datetimepickerin">
<label for="datetimepickerout" class="date-input-holder__label"><i class="glyphicon glyphicon-calendar fa fa-calendar"></i></label>
<input type="text" class="form-control datetimepicker datetimepicker--checkout" name="datetimepickerout" id="datetimepickerout">
<script>
// Date picker scripts (daterangepicker.com)
// Date from vars
var todaysdate = moment().subtract(0, 'days');
var daterangeparams = {
singleDatePicker: true,
"autoApply": true,
minDate : todaysdate,
"startDate" : todaysdate,
linkedCalendars: true,
locale: {
"format": 'DD/MM/YYYY',
"firstDay": 1
}
};
// Date from calendar init
jQuery('.datetimepicker--checkin').daterangepicker(daterangeparams);
jQuery('.datetimepicker--checkin').on('apply.daterangepicker', function(ev, picker) {
var depdate = picker.startDate.format('DD/MM/YYYY');
jQuery('[name="datetimepickerin"]').val(depdate);
jQuery('[name="depdate-day"]').val(picker.startDate.date());
jQuery('[name="depdate-month"]').val(picker.startDate.month() + 1);
jQuery('[name="depdate-year"]').val(picker.startDate.year());
jQuery('[name="datetimepicker"]').show();
});
// Date to vars
var startout = moment().add(7, 'days');
var daterangeparamsout = {
singleDatePicker: true,
"autoApply": true,
minDate : todaysdate,
"startDate" : startout,
linkedCalendars: true,
locale: {
"format": 'DD/MM/YYYY',
"firstDay": 1
}
};
// Date to calendar init
jQuery('.datetimepicker--checkout').daterangepicker(daterangeparamsout);
jQuery('.datetimepicker--checkout').on('apply.daterangepicker', function(ev, picker) {
var retdate = picker.startDate.format('DD/MM/YYYY');
jQuery('[name="datetimepickerout"]').val(retdate);
jQuery('[name="retdate-day"]').val(picker.startDate.date());
jQuery('[name="retdate-month"]').val(picker.startDate.month() + 1);
jQuery('[name="retdate-year"]').val(picker.startDate.year());
jQuery('[name="datetimepicker"]').show();
});
</script>
<input type="hidden" name="depdate-day" id="depdate-day" value="" />
<input type="hidden" name="depdate-month" id="depdate-month" value=""/>
<input type="hidden" name="depdate-year" id="depdate-year" value="" />
<input type="hidden" name="retdate-day" id="retdate-day" value="" />
<input type="hidden" name="retdate-month" id="retdate-month" value=""/>
<input type="hidden" name="retdate-year" id="retdate-year" value="" />