daterangepicker两个不同的输入

时间:2017-09-22 06:19:41

标签: jquery daterangepicker

我在同一个表单中有两个不同的daterangepicker,因为我需要将它放在两个输入中,也不要只放在一个..所以我用类选择器初始化它: $( '日期范围')daterangepicker({});

everithing工作完美,但我想做两个改变。如果我在第一个中选择一个范围,当我打开第二个范围时,我希望在日历中选择相同的范围。所以我想在ON SHOW活动中做这件事

$('.daterange').daterangepicker()
.on('apply.daterangepicker' ,function(ev, picker) {
$('.scheduledStartGmt').val(picker.startDate.format(picker.format)).change();
$('.scheduledEndGmt').val(picker.endDate.format(picker.format)).change();
})
.on('show.daterangepicker' ,function(ev, picker){

$('.daterange').data('daterangepicker').setStartDate($('.scheduledStartGmt').val()); 
$('.daterange').data('daterangepicker').setEndDate($('.scheduledEndGmt').val());

});

仅更改第一个。我该怎么办?

1 个答案:

答案 0 :(得分:3)

我遇到了同样的问题,但找到了解决方案,这可能对您有所帮助:

看看我的代码:

    if($('#search_checkin, #search_checkout').length){
    // check if element is available to bind ITS ONLY ON HOMEPAGE
    var currentDate = moment().format("DD-MM-YYYY");

    $('#search_checkin, #search_checkout').daterangepicker({
        locale: {
              format: 'DD-MM-YYYY'
        },
        "alwaysShowCalendars": true,
        "minDate": currentDate,
        "maxDate": moment().add('months', 1),
        autoApply: true,
        autoUpdateInput: false
    }, function(start, end, label) {
      // console.log("New date range selected: ' + start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD') + ' (predefined range: ' + label + ')");
      // Lets update the fields manually this event fires on selection of range
      var selectedStartDate = start.format('DD-MM-YYYY'); // selected start
      var selectedEndDate = end.format('DD-MM-YYYY'); // selected end

      $checkinInput = $('#search_checkin');
      $checkoutInput = $('#search_checkout');

      // Updating Fields with selected dates
      $checkinInput.val(selectedStartDate);
      $checkoutInput.val(selectedEndDate);

      // Setting the Selection of dates on calender on CHECKOUT FIELD (To get this it must be binded by Ids not Calss)
      var checkOutPicker = $checkoutInput.data('daterangepicker');
      checkOutPicker.setStartDate(selectedStartDate);
      checkOutPicker.setEndDate(selectedEndDate);

      // Setting the Selection of dates on calender on CHECKIN FIELD (To get this it must be binded by Ids not Calss)
      var checkInPicker = $checkinInput.data('daterangepicker');
      checkInPicker.setStartDate(selectedStartDate);
      checkInPicker.setEndDate(selectedEndDate);

    });

} // End Daterange Picker

如果您要我解释,请告诉我。