如何在descanding顺序中显示jQuery datepicker年份范围列表?

时间:2016-10-29 20:22:51

标签: jquery datepicker

演示:https://jsfiddle.net/a79dvqug/

我的问题是如何以降序显示年份列表而不是提升?

    var d = new Date();
            var dDate = d.getDate()+"-"+d.getMonth()+"-"+(d.getFullYear()-18);

 jQuery( "#date2" ).datepicker({
               yearRange: "-100:-18",
               dateFormat: 'dd-mm-yy',
               changeMonth: true,
               changeYear: true,
               defaultDate: dDate,
               onSelect: function(dateText){
               alert(dDate);
                             alert(dateText); 
              }

            });

1 个答案:

答案 0 :(得分:0)

我设法提出了一个非常难看的解决方案。如果有人有更好的扩展datepicker。我没有使用datepicker内部代码来实现这一目标。

演示:https://jsfiddle.net/q0h4s89h/1/

 var d = new Date();
 var dDate = d.getDate()+"-"+(d.getMonth()+1)+"-"+(d.getFullYear()-18);

  jQuery( "#date2" ).datepicker({
           yearRange: "-100:-18",
           dateFormat: 'dd-mm-yy',
           changeMonth: true,
           changeYear: true,
           defaultDate: dDate,
           onSelect: function(dateText){
                         alert(dDate);
                         alert(dateText); 
          }

        });



 function modifyDatepickerYears() {

    var ln = jQuery('.ui-datepicker-year option').length;
    var newHtmlOptions = "";
    /// iterate the year options
    for (var i=ln-1;i>=0;i--){
            var item =jQuery('.ui-datepicker-year option:eq('+i+')');
      /// get the option html
      var itemHtml = jQuery('<div>').append(item.clone()).html();
                newHtmlOptions += itemHtml;
    }
    jQuery('.ui-datepicker-year').html(newHtmlOptions);
    jQuery('.ui-datepicker-year').addClass('ordered')
 }

 function checkDatepickers(){
        var ln = jQuery('.ui-datepicker-year option').length;
        if (ln) {

            if (!jQuery('.ui-datepicker-year').hasClass('ordered')) {
                console.log('Unordered Date picker options found! We have: '+ln);
                modifyDatepickerYears();
            }
            // console.log('Exitings');
            //clearInterval(timer)

        }

 }

 var timer = setInterval(function(){
            checkDatepickers();
        },1000);