使用jquery触发另一个datetimepicker

时间:2017-10-28 13:48:11

标签: javascript jquery datepicker datetimepicker eonasdan-datetimepicker

我想在另一个选择器中选择日期后打开datepicker。为此,我们可以在datepicker中看到很多建议和答案,但我找不到datetimepicker的解决方案。我在项目中使用的默认datetimepicker

$('.datepicker').datetimepicker({
    format: 'MM/DD/YYYY',
    icons: {
        time: "fa fa-clock-o",
        date: "fa fa-calendar",
        up: "fa fa-chevron-up",
        down: "fa fa-chevron-down",
        previous: 'fa fa-chevron-left',
        next: 'fa fa-chevron-right',
        today: 'fa fa-screenshot',
        clear: 'fa fa-trash',
        close: 'fa fa-remove',
        inline: true
    }
});

$( "#QuoteDate" ).datepicker({
    minDate: 0, 
    changeMonth: true,
    changeYear: true,
    maxDate: "+2Y",
    onSelect: function( selectedDate ) {
        $( "#DueDate" ).datepicker("option", "minDate", selectedDate );
        setTimeout(function(){
            $( "#DueDate" ).datepicker('show');
        }, 16);
    }
});
$( "#DueDate" ).datepicker({
    changeMonth: true,
    changeYear: true,
    maxDate: "+2Y",
});

如果我尝试像onSelect那样进行编码:就像在datepicker中一样,它不接受。请有人帮我找到解决方案。此选择器中的onSelect日期将调用另一个选择器。

2 个答案:

答案 0 :(得分:1)

如果您需要关联datetimepicker,请查看文档中的Linked Pickers示例。

您必须向dp.change事件添加一个列表器,因为datetimepicker没有onSelect选项。请注意dp.change

  

更改日期时触发。

     

参数:

e = {
    date, //date the picker changed to. Type: moment object (clone)
    oldDate //previous date. Type: moment object (clone) or false in the event of a null
}

您可以使用show()功能将第二个datetimepicker打开。

这是一个完整的实例:



var icons = {
  time: "fa fa-clock-o",
  date: "fa fa-calendar",
  up: "fa fa-chevron-up",
  down: "fa fa-chevron-down",
  previous: 'fa fa-chevron-left',
  next: 'fa fa-chevron-right',
  today: 'fa fa-screenshot',
  clear: 'fa fa-trash',
  close: 'fa fa-remove',
  //inline: true
};

$('#QuoteDate').datetimepicker({
  format: 'MM/DD/YYYY',
  useCurrent: false,
  minDate: moment().startOf('day'),
  maxDate: moment().add(2, 'years'),
  icons: icons
}).on('dp.change', function(e){
  console.log('change', e.date, e.oldDate);
  if( 
    ( e.date && !e.oldDate ) ||
    ( e.date && e.oldDate && !e.date.isSame(e.oldDate, 'day') )
  ){
    setTimeout(function(){
      $('#DueDate').data("DateTimePicker").show();
    }, 16);
  }
  $('#DueDate').data("DateTimePicker").minDate(e.date);
});

$('#DueDate').datetimepicker({
  format: 'MM/DD/YYYY',
  useCurrent: false,
  minDate: moment().startOf('day'),
  maxDate: moment().add(2, 'years'),
  icons: icons
}).on('dp.change', function(e){
  $('#QuoteDate').data("DateTimePicker").maxDate(e.date);
});

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet"/>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js"></script>

<div class='col-sm-6'>
  <div class="form-group">
    <div class='input-group date' id='QuoteDate'>
      <input type='text' class="form-control" />
      <span class="input-group-addon">
        <span class="glyphicon glyphicon-calendar"></span>
      </span>
    </div>
  </div>
</div>

<div class='col-sm-6'>
  <div class="form-group">
    <div class='input-group date' id='DueDate'>
      <input type='text' class="form-control" />
      <span class="input-group-addon">
        <span class="glyphicon glyphicon-calendar"></span>
      </span>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

由于您使用的是datetimepicker,因此您可以使用minDate: moment().startOf('day')代替minDate: 0maxDate: moment().add(2, 'years')代替maxDate: "+2Y"(请参阅startOf()和{{3} } docs更多信息)。

正如add()示例中所建议的那样,您必须使用Linked Pickers选项。

答案 1 :(得分:-1)

我认为你正在寻找change事件,所以当你在其中一个日期选择器中选择了一个日期时,你可以获得一个被更改的回调,之后在openAnotherDatePicker函数中,你可以尝试从使用jquery .val()函数的事件中获取所选值,并将所选日期传递给下一个日期选择器

更新:这是xdan datetimepicker的实时和工作示例:

&#13;
&#13;
var setMinDate = function( currentDateTime ){
	this.setOptions({minDate:currentDateTime});
};

$('#QuoteDate').datetimepicker({
  format:'d-m-Y h:i',
  onChangeDateTime:openAnotherDatePicker,
  closeOnDateSelect:true
 });
$('#DueDate').datetimepicker({
  format:'d-m-Y h:i',
  onShow:setMinDate,
  closeOnDateSelect:true
 })

function openAnotherDatePicker( selectedDate ) {
	$( "#DueDate" ).val($( "#QuoteDate" ).val());
	$( "#DueDate" ).datetimepicker('show');
}
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.4/build/jquery.datetimepicker.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.4/build/jquery.datetimepicker.full.min.js"></script>

<p>
Start <br /><input id="QuoteDate" type="text" value="12-11-2017 06:24">
<br />
End <br /><input id="DueDate" type="text" value="13-11-2017 06:24">
</p>
&#13;
&#13;
&#13;