jQuery Datetimepicker - change minDateTime and maxDateTime on click?

*爱你&永不变心* 提交于 2019-12-12 12:26:59

问题


I'm using the excellent Trent Richardson datetimepicker addon to the jQuery UI datepicker.

I have rows of data with two instances of the picker on each row, one for the start and one for the end time of a process. When my user clicks one, I'd like to reset the min or max datetime element based on the value of the other box - in other words, limit the end time based on the start time and the start time based on the end time (to prevent the user from starting after end or ending before start).

I invoke the datetimepicker like this:

$('.c_inbox_date').datetimepicker({
    hourGrid: 12,
    minuteGrid: 30,
    timeFormat: 'HHmm',
    showTimezone: false,
})

Here's the function I'm working on to reset the min and max times on the fly:

$('.c_inbox_date').click(function() {
    var this_col = $(this).closest('td').parent().children().index($(this).closest('td'));  
    if (this_col == 3) {
        var stop_time = $(this).closest('td').next('td').children('input').val();       
        $(this).datetimepicker({
            maxDateTime: stop_time
        })              
    } else {    
        var start_time = $(this).closest('td').prev('td').children('input').val();
        $(this).datetimepicker({
            minDateTime: new Date(start_time)
        })              
    }
})

Alert boxes in the function confirm that the start_time and stop_time variables contain the correct values. As you can see, I tried using 'new Date' to make sure the value was actually a date object, but that didn't work either.

Can anyone tell me why this doesn't work? Does datetimepicker allow this kind of on-the-fly change? Or is my syntax messed up? Any help is, as always, much appreciated.


回答1:


Use both of them.

$('input')
  .datepicker('option', 'maxDate', new Date())
  .datetimepicker('option', 'maxDateTime', new Date());

The joys of reverse engineering...




回答2:


When setting the date back, with just the normal datepicker, I use this syntax:

$( this ).datepicker( "option", "maxDate", new Date(your_date) );
//your_date is a string in the format: YYYY,MM,DD

I would assume that in your case, you could use:

$( this ).datetimepicker( "option", "maxDateTime", new Date(your_date) );
//your_date is a string in the format: YYYY,MM,DD,HH,II


来源:https://stackoverflow.com/questions/14361266/jquery-datetimepicker-change-mindatetime-and-maxdatetime-on-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!