我在我的网络应用中使用https://github.com/Eonasdan/bootstrap-datetimepicker来显示datetimepicker。
我有2个控件 - startDate and endDate
我想将minDate
的{{1}}设置为大于'endDate'
。
此插件提供'startDate'
选项。
但是,如何从另一个datetimepicker字段设置它的值?
我试着做如下。
'minDate'
但它没有用。
有人可以提供一些指示吗?
答案 0 :(得分:0)
您可以使用$("#start-date-time-datepicker").data("DateTimePicker").date()
或$("#start-date-time-datepicker").find("input").val()
来获取所需的值。
这是一个简单的演示。
var $start = $('#start-date-time-datepicker'),
$end1 = $('#end-date-time-datepicker1'),
$end2 = $('#end-date-time-datepicker2')
$start.datetimepicker({
minDate: moment(),
sideBySide: true,
daysOfWeekDisabled: [0,6]
});
$end1.datetimepicker({
minDate: $start.data("DateTimePicker").date(),
sideBySide: true,
daysOfWeekDisabled: [0,6]
});
$end2.datetimepicker({
// note that this is not in a recognized RFC2822 or ISO format
// $("#start-date-time-datepicker").find("input").val()
minDate: new Date($start.find("input").val()),
sideBySide: true,
daysOfWeekDisabled: [0,6]
});
$start.on('dp.change', function(){
// $start input change event
var startDate = $(this).data("DateTimePicker").date();
// set $end1 and $end2 to $start's date and set the minDate
$end1.data("DateTimePicker").options({
date: startDate,
minDate: startDate
})
$end2.data("DateTimePicker").options({
date: startDate,
minDate: startDate
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js"></script>
<div class='input-group date' id='start-date-time-datepicker'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<div class='input-group date' id='end-date-time-datepicker1'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<div class='input-group date' id='end-date-time-datepicker2'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
答案 1 :(得分:0)
您需要绑定日期选择器
$(function () {
$('#start_date').datetimepicker({
// custom config
});
$('#end_date').datetimepicker({
// custom config
});
$("#start_date").on("dp.change", function (e) {
$('#end_date').data("DateTimePicker").minDate(e.date);
});
$("#end_date").on("dp.change", function (e) {
$('#start_date').data("DateTimePicker").maxDate(e.date);
});
});
参考:http://eonasdan.github.io/bootstrap-datetimepicker/#linked-pickers