Datetimepicker - 理解代码时遇到麻烦

时间:2012-05-10 06:48:30

标签: jquery jquery-ui datepicker datetimepicker

我正在审查我以前用于未完成项目的旧代码,我无法理解它的一部分,你能帮我理解吗?我正在使用http://trentrichardson.com/examples/timepicker/中支持jQuery UI日期选择器的datetimepicker。这是代码(这是我不理解的onSelect事件代码):

    $(function() {
    var dates = $( "#from, #to" ).datetimepicker({
        changeMonth: true,
        onSelect: function( selectedDate ) {
            var option = this.id == "from" ? "minDate" : "maxDate",
                instance = $( this ).data( "datepicker" ),
                date = $.datepicker.parseDate(
                    instance.settings.dateFormat ||
                    $.datepicker._defaults.dateFormat,
                    selectedDate, instance.settings );
            dates.not( this ).datepicker( "option", option, date );
        },
        });
    });

1 个答案:

答案 0 :(得分:1)

如果我没错。有2个元素#from and #to,如果用户在#from元素上选择日期值,则jquery函数将选择的值确定为#to元素的minDate
如果用户为#to选择日期值,则函数将确定#from的maxDate为给定的选定日期。

总结一下,该功能是通过选择另一个元素来确定元素的minDatemaxDate

var option = this.id == "from" ? "minDate" : "maxDate", //determine datepicker based on elemen selected
                instance = $( this ).data( "datepicker" ),
                date = $.datepicker.parseDate(
                    instance.settings.dateFormat ||
                    $.datepicker._defaults.dateFormat,
                    selectedDate, instance.settings );  // retrieve selected datepicker data 
            dates.not( this ).datepicker( "option", option, date );  //select another of this element then set minDate or maxDate of datepicker data into the element