我使用的jQuery Timepicker:http://timepicker.co/
我想要实现的目标:
$('#t1').timepicker({
timeFormat : 'hh:mm a',
interval : 30,
minTime : '8',
maxTime : '11:00 PM',
startTime : '08:00 AM',
dynamic : false,
dropdown : true,
scrollbar : true
});
$('#t1')
.timepicker('option', 'change', function(time) {
$('#t2').timepicker('option', 'minTime', time);
$('#t2').timepicker('setTime', time);
});
$('#t2').timepicker({
timeFormat : 'hh:mm a',
interval : 30,
maxTime : '11:00 PM',
startTime : '08:00 AM',
dynamic : false,
dropdown : true,
scrollbar : true
});
现在一切都运作良好,除了加入2小时的不稳定方式。
答案 0 :(得分:3)
使用传递给Date
更改处理程序的time
对象(#t1
);在此时间内添加2小时(以毫秒为单位,因此2 * 60 * 60 * 1000
),并创建一个新的Date()
对象。根据 设置#t2
。
$('#t1')
.timepicker('option', 'change', function(time) {
var later = new Date(time.getTime() + (2 * 60 * 60 * 1000));
$('#t2').timepicker('option', 'minTime', time);
$('#t2').timepicker('setTime', later);
});
$('#t1').timepicker({
timeFormat: 'hh:mm a',
interval: 30,
minTime: '8',
maxTime: '11:00 PM',
startTime: '08:00 AM',
dynamic: false,
dropdown: true,
scrollbar: true
});
$('#t1')
.timepicker('option', 'change', function(time) {
var later = new Date(time.getTime() + (2 * 60 * 60 * 1000));
$('#t2').timepicker('option', 'minTime', time);
$('#t2').timepicker('setTime', later);
});
$('#t2').timepicker({
timeFormat: 'hh:mm a',
interval: 30,
maxTime: '11:00 PM',
startTime: '08:00 AM',
dynamic: false,
dropdown: true,
scrollbar: true
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/timepicker/1.3.5/jquery.timepicker.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/timepicker/1.3.5/jquery.timepicker.min.css" rel="stylesheet" />
<input type="text" id="t1" />
<input type="text" id="t2" />
答案 1 :(得分:2)
我自己找到了答案。工作得很好:
// TimePicker
$('#t1').timepicker({
timeFormat : 'hh:mm a',
interval : 30,
minTime : '8',
maxTime : '11:00 PM',
startTime : '08:00 AM',
dynamic : false,
dropdown : true,
scrollbar : true
});
$('#t1')
.timepicker('option', 'change', function(time) {
var startHour = parseInt($('#t1').val().substring(0, 2));
var startMinutes = $('#t1').val().substring(2, 8);
var endMinTime = (startHour + 2).toString().concat(startMinutes);
$('#t2').timepicker('option', 'minTime', endMinTime);
$('#t2').timepicker('setTime', endMinTime);
});
$('#t2').timepicker({
timeFormat : 'hh:mm a',
interval : 30,
maxTime : '11:00 PM',
startTime : '08:00 AM',
dynamic : false,
dropdown : true,
scrollbar : true
});