9

我正在使用 daterangepicker bootstrap 3。

$('#pa_date*').daterangepicker({
                singleDatePicker: true,
                showDropdowns: true,
                minDate: min,
                maxDate: max,
                format: 'DD/MM/YYYY'
            }).on('apply.daterangepicker', function (ev, picker) {
                alert(picker.startDate.format('MM/YYYY'));
            });

它向我展示了完整的日期范围选择器:

在此处输入图像描述

我想隐藏日历并仅显示年份和月份下拉菜单,如下所示:

在此处输入图像描述

4

2 回答 2

9

只需添加以下 css ...

$('input[name="daterange"]').daterangepicker({
  singleDatePicker: true,
  showDropdowns: true,
  minDate: '06/01/2013',
  maxDate: '06/30/2015',      
  format: 'DD/MM/YYYY'
}).on('hide.daterangepicker', function (ev, picker) {
  $('.table-condensed tbody tr:nth-child(2) td').click();
  alert(picker.startDate.format('MM/YYYY'));
});
.table-condensed thead tr:nth-child(2),
.table-condensed tbody {
  display: none
}
<script src="https://code.jquery.com/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script type="text/javascript" src="//cdn.jsdelivr.net/momentjs/2.9.0/moment.min.js"></script>
<script type="text/javascript" src="//cdn.jsdelivr.net/bootstrap.daterangepicker/1/daterangepicker.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/bootstrap.daterangepicker/1/daterangepicker-bs3.css" />

<input name="daterange" value="DD/MM/YYYY">

于 2015-06-03T12:10:17.340 回答
2

我正在对以前的答案进行一些更改,它适用于较新的版本,我在网上添加了一些重要的注释。

jsfiddle.net/game5413/snc1Lowf/2/

var td = $(instance.container).find('.table-condensed tbody tr:nth-child(3) td:first-child');
    /*
     * the setTimeout have on purpose to delay calling trigger
     * event when choosing date on third row, if you not provide
     * the timeout, it will throw error maximum callstack
     */
    setTimeout(function() {
      /*
       * on the newer version to pick some date was changed into event
       * mousedown
       */
      td.trigger('mousedown');
      /*
       * this was optional, because in my case i need send date with
       * starting day with 1 to keep backend neat
       */
      instance.setStartDate(instance.startDate.date(1));
      instance.setEndDate(instance.endDate.date(1));
      alert("this is start " + instance.startDate.format("DD MMM YYYY"));
      alert("this is end " + instance.endDate.format("DD MMM YYYY"));
    }, 1);

您可以在我提供的链接上查找详细信息

于 2020-10-11T17:31:59.560 回答