本文介绍了重置并将日期保留为datepicker中的默认日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的HTML代码

<div style="display: inline-block; font-weight: bold;">
    <label for="fromDateRange">From:</label>
    <div id="fromDateRange"></div>
</div>

<div style="margin-left:20px; display: inline-block; font-weight: bold;">
    <label for="toDateRange">Till:</label>
    <div id="toDateRange"></div>
</div>

我的javascript: -

My javascript:-

$('#toDateRange').datepicker({
    // minDate: $( "#fromDateRange" ).datepicker( "getDate" ),
    changeMonth: true,
    changeYear:true,
    numberOfMonths: 1,
    dateFormat: "yy-mm-dd",
    onSelect: function( selectedDate ) {
        $( "#fromDateRange" ).datepicker( "option", "maxDate", selectedDate );
    }
});


$( "#fromDateRange").datepicker({
     defaultDate: "-1m",
     changeMonth: true,
     changeYear:true,
     numberOfMonths: 1,
     dateFormat: "yy-mm-dd",
     onSelect: function( selectedDate ) {
         $( "#toDateRange" ).datepicker( "option", "minDate", selectedDate );
    }
});

清除日期功能:

function clearDates() {
    clear="";
    $('#dateFilter').val(clear);
    $("#dateFilter").trigger("change");
    $('#date-range').hide();
}

一旦我清除日期选择器中的活动日期应该是默认日期。如何做到这一点?

Once I clear the active dates in the date picker should be the default one.How Do I do it?

推荐答案

如果我对你有正确的意思,而你真的希望在用户使用时设置日期为默认值 clearDates()你必须存储给出的信息。

If i got you right and you really want to set a date as default when the user uses clearDates() you have to store the Informations that was given.

为此,你必须要...

For this you have to ways...

Way One(LocalStorage):

//set value to browser cache
    localStorage.setItem("key", "value");
//get from browser cache
    localStorage.getItem("key"); //returns the setted value
//remove specific entry from browser cache
    localStorage.removeItem("key"); //delete entry xy
//remove all from browser cache
    localStorage.clear();

方式二:

使用cookys。

这篇关于重置并将日期保留为datepicker中的默认日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:09