0

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.

Example
Start time: <input type="text" id="startTime" />
End time: <input type="text" id="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.

4

1 回答 1

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]);
    }
});
于 2013-02-10T01:21:28.923 回答