如何在多个jQueryUI Datepickers中清除特定日期

时间:2015-04-09 00:32:13

标签: javascript jquery jquery-ui datepicker jquery-ui-datepicker

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

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/

2 个答案:

答案 0 :(得分:0)

以下是有关如何使用@charlietfl和@Emm提供的有用注释的示例。可以找到小提琴here

但基本上你保留你的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
    });
});

答案 1 :(得分:0)

搞定了!检查下面的代码

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>