Bootstrap Datepicker默认为粘性日期

时间:2013-10-11 18:47:06

标签: javascript jquery css twitter-bootstrap datepicker

我想知道是否有人编写了一个优雅的解决方案,默认情况下,空白Bootstrap Datepicker使用DOM / UI中另一个datepicker元素之前选择的日期(而不是默认为每个新实例的今天日期)。到目前为止,这是我的解决方案 - 使用jQuery cookie:

//multiple elements with class 'DatePicker'
$('.DatePicker').datepicker({autoclose:true}).on('show',function() {

    //getting cookie of last datepicker, if it exists
    lastDate = $.cookie('lastDate') ? new Date($.cookie('lastDate')) : null;

    //checking for blank DP and cookie combo, otherwise today is default
    if ($(this).val() == '' && lastDate) { 

         //adding calendar day to date, as DP 'changeDate' event sets object to previous day 
         lastDate.setDate(lastDate.getDate()+1);

         //overriding today-as-default behavior
         $(this).datepicker('setDate',lastDate).datepicker('update');

    }
}).on('changeDate',function(ev) {

    //getting and setting cookie
    selectedDate = ev.date;
    $.cookie('lastDate',selectedDate);

});

0 个答案:

没有答案