使用jquery在开始时间上添加一小时

时间:2013-02-10 01:06:00

标签: jquery time

我有两个文本框。一个是开始时间,另一个是结束时间。当用户在开始时间输入时间时,如何自动在结束时间框中添加一小时?

值为字符串,数据类型不为time。我真的找不到互联网上有关如何做到这一点的任何事情。

示例
Start time: <input type="text" id="startTime" />
End time: <input type="text" id="endTime" />

如果用户在startTime中输入14:00,我需要它在15:00自动填充endTime

关于如何做到这一点的任何想法?谢谢你。

1 个答案:

答案 0 :(得分:2)

I think this should work for you considering the above comments and your requirements

$('#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]);
    }
});