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

我只是一个虾纸丫 提交于 2019-12-06 01:56:56

问题


i have code on the onSelect event of the jquery ui datepicker and 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.

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

回答1:


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();
            }
        }
});



回答2:


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

$('#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.



来源:https://stackoverflow.com/questions/5751584/in-jquery-ui-datepicker-on-the-onselect-event-is-there-anyway-to-get-the-prev

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!