使用jquery在开始时间上增加一小时 [英] Add one hour onto start time using jquery
问题描述
我有两个文本框.一个是开始时间,另一个是结束时间.当用户在开始时间中输入时间时,如何自动在结束时间框中添加一小时?
这些值是字符串,并且不具有time
的数据类型.我真的在互联网上找不到有关如何执行此操作的任何信息.
I have two textboxes. One is for a start time and the other is an end time. How can I add one hour to the end time box automatically when a user enters a time in the start time?
The values are strings and do not have a datatype of time
. I really cannot find anything on the internet on how to go about doing this.
示例
Start time: <input type="text" id="startTime" />
End time: <input type="text" id="endTime" />
Example
Start time: <input type="text" id="startTime" />
End time: <input type="text" id="endTime" />
如果用户在startTime
中输入14:00,我需要它以15:00自动填充endTime
.
If the user enters 14:00 in startTime
, I need it to automatically populate endTime
with 15:00.
关于如何做到这一点的任何想法?谢谢.
Any ideas on how this can be done? Thanks you.
推荐答案
$('#startTime').keyup(function(){
if(/[0-9]{2}\:[0-9]{2}/.test($(this).val())){
var startTime = $(this).val().split(':');
var endHours = parseInt(startTime[0]) +1;
endHours = Math.min(Math.max(endHours, 1), 24);
$('#endTime').val(endHours +':'+ startTime[1]);
}
});
这篇关于使用jquery在开始时间上增加一小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!