简体   繁体   中英

jQuery datepicker confirm dialog

I have this weird requirement with jQuery datepicker . I have a datepicker attached to an input, which already has dates fetched in from database. Now what I am looking to achieve is once I click on the input box the datepicker appears (default functionality of datepicker) and when I click on any other dates, it want a simple JavaScript confirm box with OK and Cancel , so clicking OK will change the dates and clicking Cancel will not.

Any idea how to do that?. I was able to create the confirm box and attach it to onSelect event from the datepicker . But the datepicker still changed dates even after clicking Cancel .

$('.date').datepicker({
        changeYear: true,
        changeMonth: true,
        onSelect: function (e) {
               var popup = confirm("Do you want to change dates?");
               if (popup == true) {

               } else {
                  return false
               }
           }
        }
    });

Store the previous value in data attribute and when canceled set the previous value. Try this

$( document ).ready(function() {
    $('.date').data("prev", $(this).val());
    $('.date').datepicker({
        changeYear: true,
        changeMonth: true,
        onSelect: function (e) {
           var prevDate = $(this).data("prev");
           var popup = confirm("Do you want to change dates?");
           if (popup == true) {
                $('.date').data("prev", $(this).val());
           } else {
              $( ".date" ).datepicker('setDate',prevDate);
           }
       }
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM