【问题标题】:How to Blackout Specific Dates Across Multiple jQueryUI Datepickers如何在多个 jQueryUI 日期选择器中屏蔽特定日期
【发布时间】:2023-04-10 06:11:02
【问题描述】:

我想在单个页面上的四个不同的 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/

【问题讨论】:

  • 你可以在这里找到答案stackoverflow.com/questions/15400775/…
  • 不需要重复所有这些,传递多个选择器并调用一次 datepicker
  • Emm - 我看到了,但我认为我把代码放错了。你能提供一个如何使用它的例子吗?
  • charlietfl - 我是编码新手,你能提供一个例子吗?
  • $( "#datepicker, #datepicker2, #datepicker3" ).datepicker({ /* options */})

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


【解决方案1】:

这是一个关于如何使用 @charlietfl 和 @Emm 提供的有用 cmets 的示例。小提琴可以找到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
    });
});

【讨论】:

    【解决方案2】:

    开始工作!检查下面的代码

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-21
      • 2020-01-11
      • 1970-01-01
      • 2014-12-06
      相关资源
      最近更新 更多