JQuery DatePicker和beforeShowDay

use*_*124 11 jquery datepicker

我拼命地尝试使用此功能仅启用我的datepicker中的特定日期,但该beforeShowDay功能永远不会被触发:(

即使这不起作用:

$(document).ready(function(){
/*initialisation des composants*/
initComponent();
});


availableDates = new Array();

/* Fonction d'initialisation des composants */
function initComponent(){

/* Date retrait */
$("#dateRetrait").datepicker();

$("#dateRetrait").datepicker({beforeShowDay: function(d) {
        console.log("bsd");
        alert("bsd");
    }});

//$("#dateRetrait").datepicker({buttonImage: "../../../Images/boutons/btn_calendier.png"});
//$("#dateRetrait").datepicker({showButtonPanel: true });
//$("#dateRetrait").datepicker({beforeShow: function() {setTimeout(function() {$(".ui-datepicker").css("z-index", 9999999999);}, 10);}});

$('#comboLieux').attr('disabled', 'disabled');
$('#comboCreneau').attr('disabled', 'disabled');
$('#dateRetrait').attr('disabled', 'disabled');
$('#dateRetrait').datepicker('option', 'minDate', new Date());

$("#dateRetrait").datepicker("option","dateFormat", 'dd-mm-yy');
}
Run Code Online (Sandbox Code Playgroud)

如果您有任何想法,我将不胜感激!

事实上,即使这不起作用:

<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Datepicker - Restrict date range</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function() {
$( "#datepicker" ).datepicker({ minDate: -20, maxDate: "+1M +10D" });
$( "#datepicker" ).datepicker({beforeShowDay: function(d) {
        console.log(d);
        alert(d);
    }});
});
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker" /></p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Dom*_*Dom 30

根据jQueryUI的Datepicker API,

在此输入图像描述

这解释了原因

$("#dateRetrait").datepicker({beforeShowDay: function(d) {
        console.log("bsd");
        alert("bsd");
    }});
Run Code Online (Sandbox Code Playgroud)

不起作用.

此外,我注意到你正在.datepicker()多次呼叫,每次你给它不同的参数.

代替:

$("#dateRetrait").datepicker();

$("#dateRetrait").datepicker({beforeShowDay: function(d) {
        console.log("bsd");
        alert("bsd");
    }});

$('#dateRetrait').datepicker('option', 'minDate', new Date());

$("#dateRetrait").datepicker("option","dateFormat", 'dd-mm-yy');
Run Code Online (Sandbox Code Playgroud)

试着这样做:

$("#dateRetrait").datepicker({
    dateFormat: 'dd-mm-yy',
    minDate: new Date(), 
    beforeShowDay: function(d) {
        var dmy = (d.getMonth()+1); 
        if(d.getMonth()<9) 
            dmy="0"+dmy; 
        dmy+= "-"; 

        if(d.getDate()<10) dmy+="0"; 
            dmy+=d.getDate() + "-" + d.getFullYear(); 

        console.log(dmy+' : '+($.inArray(dmy, availableDates)));

        if ($.inArray(dmy, availableDates) != -1) {
            return [true, "","Available"]; 
        } else{
             return [false,"","unAvailable"]; 
        }
    }
    });
Run Code Online (Sandbox Code Playgroud)

我还为您提供了一个演示:http://jsfiddle.net/yTMwu/18/.希望这可以帮助!


Vas*_*ryk 5

Dom 给出了很好的解释。我想添加更多缩短代码:

如果您有以下格式的可用日期数组:0000-00-00 (yyyy-mm-dd)

var availableDates = ["2020-01-05", "2020-01-10", "2020-01-14"];

$("#dateRetrait").datepicker({
    dateFormat: 'dd-mm-yy',
    minDate: new Date(), 
    beforeShowDay: function(d) {        
        var year = d.getFullYear(),
            month = ("0" + (d.getMonth() + 1)).slice(-2),
            day = ("0" + (d.getDate())).slice(-2);

        var formatted = year + '-' + month + '-' + day;

        if ($.inArray(formatted, availableDates) != -1) {
            return [true, "","Available"]; 
        } else{
            return [false,"","unAvailable"]; 
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

beforeShowDay - 每次都有效,在渲染日之前))

$.inArray - 在数组中查找值(如果没有值则为-1)

在此解决方案中,您可以制作stopDates,只需更改行:

if ($.inArray(formatted, availableDates) != -1) {
Run Code Online (Sandbox Code Playgroud)

if ($.inArray(formatted, availableDates) == -1) {
Run Code Online (Sandbox Code Playgroud)