在 jquery ui datepicker 中,在 OnSelect() 事件上无论如何都可以获取上一个选定的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5751584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:46:18  来源:igfitidea点击:

in jquery ui datepicker, on the OnSelect() event is there anyway to get the previous selected date

jqueryjquery-ui-datepicker

提问by leora

i have code on the onSelectevent of the jquery ui datepickerand i now want to only run my function if the date has changed values (so if a user selects a date that was already there in the textbox, i don't want to run this code as it will be a redundant calculation). Here is my existing code.

我在jquery ui datepickeronSelect事件上有代码,现在我只想在日期更改值时运行我的函数(因此,如果用户选择文本框中已经存在的日期,我不想运行这段代码,因为这将是一个冗余计算)。这是我现有的代码。

$('#Milestone').datepicker({
    dateFormat: 'dd M yy',
    onSelect: calcUpdate
});

回答by Reza Mamun

According to http://api.jqueryui.com/datepicker/#option-onSelect, you should try this:

根据http://api.jqueryui.com/datepicker/#option-onSelect,你应该试试这个:

$('#Milestone').datepicker({
    dateFormat: 'dd M yy',
    onSelect: function(curDate, instance){
        if( curDate != instance.lastVal ){
            //so, the date is changed;
            //Do your works here...
        }
    }
});

The onSelect function receives 2 parameters which are used here; You can debug/console the values of the 2nd parameter to know more about it.

onSelect 函数接收这里使用的 2 个参数;您可以调试/控制台第二个参数的值以了解更多信息。

回答by Chandu

You can use data to store previously stored value and compare the current value to it.

您可以使用数据来存储先前存储的值并将当前值与其进行比较。

Try this(put these statements in your document ready event):

试试这个(把这些语句放在你的文档就绪事件中):

$('#Milestone').data("prev", $(this).val());
$('#Milestone').datepicker({
    dateFormat: 'dd M yy',
    onSelect: function(dateText){
            var prevDate = $(this).data("prev")
            var curDate = dateText;
            if(prevDate == curDate){
              $(this).data("prev", curDate)
                calcUpdate();
            }
        }
});