如何在多个jQueryUI日期选择器中屏蔽特定日期

How to Blackout Specific Dates Across Multiple jQueryUI Datepickers

本文关键字:日期 选择器 屏蔽 jQueryUI      更新时间:2023-09-26

我想在一个页面上屏蔽四个不同JQuery日期选择器上的特定日期(例如5-20-2015到5-25-2015)。最简单的方法(轻量级)是什么?

HTML

<label>Date Picker 1</label><input type="text" id="datepicker" name="Datepicker" style="border-radius:5px; font-family: sans-serif; color:#333; font-size:14px; margin-bottom:5px;">
<label>Date Picker 2</label><input type="text" id="datepicker22" name="Datepicker2" style="border-radius:5px; font-family: sans-serif; color:#333; font-size:14px; margin-bottom:5px;">
<label>Date Picker 3</label><input type="text" id="datepicker3" name="Datepicker3" style="border-radius:5px; font-family: sans-serif; color:#333; font-size:14px; margin-bottom:5px;">
<label>Date Picker 4</label><input type="text" id="datepicker4" name="Datepicker4" style="border-radius:5px; font-family: sans-serif; color:#333; font-size:14px; margin-bottom:5px;">

jQuery

<script>
$(function() {
$( "#datepicker" ).datepicker({
        minDate:3,
        maxDate:180,
        dateFormat:"DD, d MM, yy",
        changeMonth:true,
        showButtonPanel:true
    });
});
 $(function() {
$( "#datepicker2" ).datepicker({
        minDate:3,
        maxDate:180,
        dateFormat:"DD, d MM, yy",
        changeMonth:true,
        showButtonPanel:true
    });
});
 $(function() {
$( "#datepicker3" ).datepicker({
        minDate:3,
        maxDate:180,
        dateFormat:"DD, d MM, yy",
        changeMonth:true,
        showButtonPanel:true
    });
});
 $(function() {
$( "#datepicker4" ).datepicker({
        minDate:8,
        maxDate:180,
        dateFormat:"DD, d MM, yy",
        changeMonth:true,
        showButtonPanel:true
    });
});
 </script>

此处为原始日期选择器的代码:https://jqueryui.com/datepicker/

下面是一个关于如何使用@charlietfl和@Emm提供的有用注释的示例。小提琴可以在这里找到

但基本上,您保留了HTML标记,然后为javascript做:

var unavailableDates = ["20-5-2015", "21-5-2015", "22-5-2015", "23-5-2015", "24-5-2015", "25-5-2015"];
function unavailable(date) {
    dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
    if ($.inArray(dmy, unavailableDates) < 0) {
        return [true, "", "Book Now"];
    } else {
        return [false, "", "Booked Out"];
    }
}
$(function () {
    $("#datepicker, #datepicker2, #datepicker3, #datepicker4").datepicker({
        minDate: 3,
        maxDate: 180,
        dateFormat: "DD, d MM, yy",
        changeMonth: true,
        beforeShowDay: unavailable,
        showButtonPanel: true
    });
});

开始工作了!检查下方的代码

HTML

<label>Date Picker 1</label><input type="text" id="datepicker" name="Datepicker" style="border-radius:5px; font-family: sans-serif; color:#333; font-size:14px; margin-bottom:5px;">
<label>Date Picker 2</label><input type="text" id="datepicker22" name="Datepicker2" style="border-radius:5px; font-family: sans-serif; color:#333; font-size:14px; margin-bottom:5px;">
<label>Date Picker 3</label><input type="text" id="datepicker3" name="Datepicker3" style="border-radius:5px; font-family: sans-serif; color:#333; font-size:14px; margin-bottom:5px;">
<label>Date Picker 4</label><input type="text" id="datepicker4" name="Datepicker4" style="border-radius:5px; font-family: sans-serif; color:#333; font-size:14px; margin-bottom:5px;">

jQuery

<script>
 $(function() {
     var array = ["Tuesday, 14 April, 2015","Wednesday, 15 April, 2015","Thursday, 16 April, 2015"]
$( "#datepicker, #datepicker2, #datepicker3, #datepicker4" ).datepicker({
        minDate:3,
        maxDate:180,
        dateFormat:"DD, d MM, yy",
        changeMonth:true,
        showButtonPanel:true,
        beforeShowDay: function(date){
            var string = jQuery.datepicker.formatDate('DD, d MM, yy', date);
            return [ array.indexOf(string) == -1 ]
    }
    });
});
 </script>