简体   繁体   中英

How to Select a Specific Date on Calendar to Trigger a Function with jQuery UI Datepicker

I'm trying to add a conditional statement to the jQuery UI Datepicker onChange to trigger a function if certain dates are selected from the calendar. Example

if (selecteddate == ‘01/15/2019’){
         alert('Function A Triggered');
     updateDataA();
}
else if (selecteddate == ‘01/25/2019’){
         alert('Function B Triggered');
     updateDataB();
}

I found something close to what I'm looking for, but I can't figure out how to modify the script to trigger the function by selecting a date instead of selecting the month and year dropdown to trigger the function. Hope this is possible. Any help will be greatly appreciated. Below is the script I'm trying to tweak.

$(function() {
    $( "#datepicker1" ).datepicker({
      changeMonth: true,
      changeYear: true,
      dateFormat: 'MM yy',
      altField: "#id",
      altFormat: "mm/yy",
      onChangeMonthYear: function(year, month, oDatepicker) {
            alert('Year: ' + year + ', Month: ' + month);
            updateDataD(); 
      }
    });
}); 

onSelect is called when the datepicker is selected:

onSelect: function(dateText, inst) {
    if (dateText == ‘01/15/2019’){
        // your function
    }
}

http://api.jqueryui.com/datepicker/#option-onSelect

In the onSelect method of the datepicker, the first parameter refers to the date (as text). You could use this to determine which function to fire.

 function updateDataA() { console.log("Update Data A"); } function updateDataB() { console.log("Update Data B"); } $("#calendar").datepicker({ onSelect: function (dateText) { if (dateText=== "01/15/2019") updateDataA(); else if (dateText=== "01/25/2019") updateDataB(); } }); 
 <link href="https://cdnjs.cloudflare.com/ajax/libs/datepicker/0.6.5/datepicker.min.css" rel="stylesheet" /> <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> <input id="calendar"> 

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