日期选择器开始日期和时间结束日期查询

时间:2018-05-04 06:08:29

标签: jquery datepicker jquery-ui-datepicker

我有2个日期选择器(表示申请2次),如果我从第一个日期选择器中选择特定日期(即开始日期),那么第二个日期选择器将禁用所有那些小于开始日期的日期。

//Date picker
$('.datepicker').datepicker({

  format: "yyyy/mm/dd",

});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div class="col-sm-12">

  <div class="col-sm-6 pull-left">
    From: <input type="text" name="reg_start_date" class="datepicker" size="15" />
  </div>
  <div class="col-sm-6 pull-left">
    To: <input type="text" name="reg_end_date" class="datepicker" size="15" />
  </div>
</div>

更新:

   <script>
<?= $this->Html->script(array('https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js','https://code.jquery.com/ui/1.11.4/jquery-ui.min.js','bootstrap/js/bootstrap.min.js','https://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js','plugins/select2/select2.full.min.js','plugins/input-mask/jquery.inputmask.js','plugins/input-mask/jquery.inputmask.date.extensions.js','plugins/input-mask/jquery.inputmask.extensions.js','plugins/daterangepicker/moment.js','https://code.jquery.com/ui/1.11.4/jquery-ui.min.js', 'plugins/sparkline/jquery.sparkline.min.js', 'plugins/jvectormap/jquery-jvectormap-1.2.2.min.js', 'plugins/jvectormap/jquery-jvectormap-world-mill-en.js','plugins/knob/jquery.knob.js','plugins/daterangepicker/daterangepicker.js','plugins/datepicker/bootstrap-datepicker.js','plugins/slimScroll/jquery.slimscroll.min.js', 'plugins/fastclick/fastclick.min.js','dist/js/app.min.js','dist/js/demo.js','plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.all.js')); ?>


      $(function () {
       $('.second').datepicker({
            format: "yy/mm/dd" 
        });

        $('.first').datepicker({
          format: "yy/mm/dd",
          onSelect: function(date) {
            alert('124');
            var date1 = $('.first').datepicker('getDate');
            var date = new Date(Date.parse(date1));
            date.setDate(date.getDate() + 1);
            var newDate = date.toDateString();
            newDate = new Date(Date.parse(newDate));
            $('.second').datepicker("option", "minDate", newDate);
          }
        });

      });

    </script>

3 个答案:

答案 0 :(得分:1)

您可以在第一个和第二个文本框中添加额外的课程,并在使用minDate选择第一个文本框时将onSelect提供给第二个文本框。

$('.second').datepicker({
    dateFormat: "yy/mm/dd" 
});

$(".first").datepicker({
  dateFormat: "yy/mm/dd",
  onSelect: function(date) {
    var date1 = $('.first').datepicker('getDate');
    var date = new Date(Date.parse(date1));
    date.setDate(date.getDate() + 1);
    var newDate = date.toDateString();
    newDate = new Date(Date.parse(newDate));
    $('.second').datepicker("option", "minDate", newDate);
  }
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div class="col-sm-12">

  <div class="col-sm-6 pull-left">
    From: <input type="text" name="reg_start_date" class="datepicker first" size="15" />
  </div>
  <div class="col-sm-6 pull-left">
    To: <input type="text" name="reg_end_date" class="datepicker second" size="15" />
  </div>
</div>

答案 1 :(得分:0)

您必须在reg_start_date上创建一个侦听器。如果内容发生变化,则为日期选择器 必须使用

设置reg_end_date
$("#reg_end_date").datepicker({ minDate: new Date(/*date from reg_start_date*/) });

答案 2 :(得分:0)

您必须在第一个datepicker(start)中添加一个侦听器,以便在每次更改时更新第二个{end}的minDate

这样的事情:

&#13;
&#13;
var $start = $('[name="reg_start_date"]');
var $end = $('[name="reg_end_date"]');

$('.datepicker').datepicker({
  format: 'yyyy/mm/dd'
});

$start.datepicker('option', 'onClose', function(date) {
  $end.datepicker('option', 'minDate', date);
});
&#13;
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div class="col-sm-12">

  <div class="col-sm-6 pull-left">
    From: <input type="text" name="reg_start_date" class="datepicker" size="15" />
  </div>
  <div class="col-sm-6 pull-left">
    To: <input type="text" name="reg_end_date" class="datepicker" size="15" />
  </div>
</div>
&#13;
&#13;
&#13;