Enable one specific date in jquery datepicker

前端 未结 2 1914
清歌不尽
清歌不尽 2021-01-24 19:03

I have a jQuery datepicker on my website, and I have disabled every first and second day (monday and tuesday) of the week. Also I have an array of days which are disabled (this

相关标签:
2条回答
  • 2021-01-24 19:52

    Just use the following condition inside your beforeShowDay function. It basically creates an exception for specified dates:

    var override = ["24-12-2018"];
    // ...
    var available = (dt.getDay() == 1 || dt.getDay() == 2 || vakantie.indexOf(datestring) >= 0) && (override.indexOf(datestring) === -1)
        ? false
        : true;
    return [available];
    

    var vakantie = ["25-12-2018", "26-12-2018", "27-12-2018", "28-12-2018", "29-12-2018", "30-12-2018", "31-12-2018"];
    var override = ["24-12-2018"];
    
    function nietbeschikbaar(dt) {
      var datestring = jQuery.datepicker.formatDate('dd-mm-yy', dt);
      var available = (dt.getDay() == 1 || dt.getDay() == 2 || vakantie.indexOf(datestring) >= 0) && (override.indexOf(datestring) === -1)
      ? false 
      : true;
      return [available];
    }
    
    jQuery("#cfgen-element-7-6").datepicker(jQuery.datepicker.regional["nl"]);
    jQuery("#cfgen-element-7-6").datepicker("option", "changeMonth", true);
    jQuery("#cfgen-element-7-6").datepicker("option", "changeYear", true);
    jQuery("#cfgen-element-7-6").datepicker("option", "minDate", +1);
    jQuery("#cfgen-element-7-6").datepicker("option", "firstDay", 1);
    jQuery("#cfgen-element-7-6").datepicker("option", "dateFormat", "dd-mm-yy");
    jQuery("#cfgen-element-7-6").datepicker("option", "yearRange", "-0:+70");
    jQuery("#cfgen-element-7-6").datepicker("option", "beforeShowDay", nietbeschikbaar);
    @import url("https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/blitzer/jquery-ui.min.css");
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
    
    <p>Navigate to December 2018</p>
    <p><input id="cfgen-element-7-6"></p>

    0 讨论(0)
  • 2021-01-24 19:54

    var vakantie = ["25-12-2018", "26-12-2018", "27-12-2018", "28-12-2018", "29-12-2018", "30-12-2018", "31-12-2018"];
    var enableDays = ["24-12-2018"];
    
    
    $("#datepicker").datepicker({beforeShowDay: function(dt) {
    var datestring = $.datepicker.formatDate('dd-mm-yy', dt);
        if($.inArray(datestring, enableDays) != -1) {
         return [true];
             }
        else{
         return [dt.getDay() == 1 || dt.getDay() == 2 ? false : true && vakantie.indexOf(datestring) == -1 ];
             }
    
       
        }});
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>jQuery UI Datepicker - Default functionality</title>
      <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
      <link rel="stylesheet" href="/resources/demos/style.css">
      <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    
    </head>
    <body>
     
    <p>Date: <input type="datepicker" id="datepicker"></p>
    
     
     
    </body>

    Try this..

    $("#datepicker").datepicker({beforeShowDay: function(dt) {
         var datestring = $.datepicker.formatDate('dd-mm-yy', dt);
         if($.inArray(datestring, enableDays) != -1) {
            return [true];
         }
         else{
          return [dt.getDay() == 1 || dt.getDay() == 2 ? false : true && vakantie.indexOf(datestring) == -1 ];
         }
    
    
    }});
    
    0 讨论(0)
提交回复
热议问题