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

I have code in the onSelect event of jquery ui datepicker , and now I only want to run my function if the date has changed values โ€‹โ€‹(therefore, if the user selects a date that was already there in the text box, I do not want to run this code, because it will be redundant payment). Here is my existing code.

$('#Milestone').datepicker({ dateFormat: 'dd M yy', onSelect: calcUpdate }); 
+6
source share
2 answers

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

Try this (put these statements in a 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(); } } }); 
+9
source

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

 $('#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 that are used here; You can debug / console the values โ€‹โ€‹of the second parameter to learn more about this.

+15
source

Source: https://habr.com/ru/post/886444/


All Articles