django jquery月份选择器 - “to”日期并不总是更新为“from”date

时间:2015-10-29 10:02:48

标签: jquery datetimepicker

我按照这篇文章中的示例:jquery datetime picker set minDate dynamic

enter image description here

我试过两种方法:

第一种方法

此方法仅适用于第一次选择“到”日期。也就是说,在选择一次“从”和“到”日期后,我回来重新选择“从”日期,然后“到”日期下拉列表不会相应地改变,它仍然是第一次我选择:

$('.to').datepicker({
   beforeShow: function(input, inst) {
       var mindate = $('.from').datepicker('getDate');
       $(this).datepicker('option', 'minDate', mindate);
   }
});

html:从“日期日历”中选择

     <script type="text/javascript">
        $(function() {
     $('.from').datepicker(
                    {
                        dateFormat: "yy/mm",
                        changeMonth: true,
                        changeYear: true,
                        showButtonPanel: true,                                  
                        onClose: function(dateText, inst) {
                            function isDonePressed(){
                                return ($('#ui-datepicker-div').html().indexOf('ui-datepicker-close ui-state-default ui-priority-primary ui-corner-all ui-state-hover') > -1);
                            }

                            if (isDonePressed()){
                                var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
                                var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
                                $(this).datepicker('setDate', new Date(year, month, 1)).trigger('change');

                                 $('.date-picker').focusout()//Added to remove focus from datepicker input box on selecting date
                            }
                        },

                        beforeShow : function(input, inst) {

                            inst.dpDiv.addClass('month_year_datepicker')

                            if ((datestr = $(this).val()).length > 0) {
                                year = datestr.substring(datestr.length-4, datestr.length);
                                month = datestr.substring(0, 2);
                                $(this).datepicker('option', 'defaultDate', new Date(year, month-1, 1));
                                $(this).datepicker('setDate', new Date(year, month-1, 1));
                                $(".ui-datepicker-calendar").hide();
                            }
                        }
                    })
        });                 
        </script>

我放入了https://jsfiddle.net/3w3h097c/。在小提琴中,下拉日历似乎没有出现我不知道为什么,但它确实出现在我的浏览器中。

从“到”日期日历中选择

与从“日期”日历中选择相比,唯一不同的是添加以下2个句子:

beforeShow : function(input, inst) {
    var mindate = $('.from').datepicker('getDate'); // Added sentence, the rest same
    $(this).datepicker('option', 'minDate', mindate);  //>Added sentence,the rest same

    inst.dpDiv.addClass('month_year_datepicker')                            
    if ((datestr = $(this).val()).length > 0) {
        year = datestr.substring(datestr.length-4, datestr.length);
        month = datestr.substring(0, 2);
        $(this).datepicker('option', 'defaultDate', new Date(year, month-1, 1));
        ......     

第二种方法 - 根本不起作用

为“from”和“to”添加“onSelect:function(selected)”。

<---from--->
$(function() {
     $('.from').datepicker(
                    {
                        dateFormat: "yy/mm",
                        changeMonth: true,
                        changeYear: true,
                        showButtonPanel: true,

                      <!--add onSelect here---->
                        onSelect: function(selected) {
                              $(".to").datepicker("option","minDate", selected)
                        },  
                        onClose: function(dateText, inst) {
                            function isDonePressed(){
                                return ($('#ui-datepicker-div').html().indexOf('ui-datepicker-close ui-state-default ui-priority-primary ui-corner-all ui-state-hover') > -1);
                            }

                            if (isDonePressed()){
                                var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
                                var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
                                $(this).datepicker('setDate', new Date(year, month, 1)).trigger('change');

                                 $('.from').focusout()//Added to remove focus from datepicker input box on selecting date
                            }
                        },

                        beforeShow : function(input, inst) {

                            inst.dpDiv.addClass('month_year_datepicker')

                            if ((datestr = $(this).val()).length > 0) {
                                year = datestr.substring(datestr.length-4, datestr.length);
                                month = datestr.substring(0, 2);
                                $(this).datepicker('option', 'defaultDate', new Date(year, month-1, 1));
                                $(this).datepicker('setDate', new Date(year, month-1, 1));
                                $(".ui-datepicker-calendar").hide();
                            }
                        }
                    })
        });

<!--to:-->

$(function() {
    $('.to').datepicker(
........
onSelect: function(selected) {
                           $('.from').datepicker("option","maxDate", selected)
                        },

.......

1 个答案:

答案 0 :(得分:1)

经过2天的搜索,终于找到了解决问题的好人。

http://techbrij.com/month-range-picker-jquery-ui-datepicker

$( "#from, #to" ).datepicker({ 
           changeMonth: true,
           changeYear: true,
           showButtonPanel: true,
           dateFormat: 'MM yy',            
           onClose: function(dateText, inst) { 
               var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
               var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();             
               $(this).datepicker('setDate', new Date(year, month, 1));
           },
           beforeShow : function(input, inst) {
               if ((datestr = $(this).val()).length > 0) {
                   year = datestr.substring(datestr.length-4, datestr.length);
                   month = jQuery.inArray(datestr.substring(0, datestr.length-5), $(this).datepicker('option', 'monthNames'));
                   $(this).datepicker('option', 'defaultDate', new Date(year, month, 1));
                   $(this).datepicker('setDate', new Date(year, month, 1));    
               }
               var other = this.id == "from" ? "#to" : "#from";
               var option = this.id == "from" ? "maxDate" : "minDate";        
               if ((selectedDate = $(other).val()).length > 0) {
                   year = selectedDate.substring(selectedDate.length-4, selectedDate.length);
                   month = jQuery.inArray(selectedDate.substring(0, selectedDate.length-5), $(this).datepicker('option', 'monthNames'));
                   $(this).datepicker( "option", option, new Date(year, month, 1));
               }
           }
       });
       $("#btnShow").click(function(){ 
       if ($("#from").val().length == 0 || $("#to").val().length == 0){
           alert('All fields are required');
       }
       else{
           alert('Selected Month Range :'+ $("#from").val() + ' to ' + $("#to").val());
           }
       })