我正在研究website,其中我希望禁用日历中的过去日期。
我用来制作日历的 HTML 和 JQuery 代码是:
$("#startdate_datepicker").datepicker({numberOfMonths:[1, 2]});
$("#enddate_datepicker").datepicker({numberOfMonths:[1, 2]});
<link href="https://cdn.bootcss.com/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div class="dates">
<div class="start_date" style="width:50%;margin-right:3%;">
<input readonly="readonly" class="form-control start_date mb-4" type="text" placeholder="start date" id="startdate_datepicker">
</div>
<div class="end_date" style="width:50%;margin-left:3%;">
<input readonly="readonly" class="form-control end_date mb-4" type="text" placeholder="end date" id="enddate_datepicker">
</div>
</div>
问题陈述:
我想知道应该对上面的代码进行哪些更改,以便选择开始日期之后,在选择结束日期时应该禁用开始日期之前的日期。
答案 0 :(得分:1)
您可以使用the option=beforeShowDay of JQuery UI,然后像下面的演示一样自定义每天的样式:
作为 beforeShowDay 的JQuery UI指南:
以日期为参数且必须返回数组的函数 与:
[0]: true/false indicating whether or not this date is selectable [1]: a CSS class name to add to the date's cell or "" for the default presentation [2]: an optional popup tooltip for this date
let startDateUI = $("#startdate_datepicker").datepicker({
numberOfMonths:[1, 2],
todayHighlight: true,
beforeShowDay: function (calDate) {
return calDate - new Date() < 0 ? [false, '', ''] : [true, '','']
}
});
$("#enddate_datepicker").datepicker({
numberOfMonths:[1, 2],
todayHighlight: true,
beforeShowDay: function (calDate) {
let selectedStartDate = $( "#startdate_datepicker" ).datepicker( "getDate" )
return calDate - selectedStartDate < 0 ? [false, 'disable-day', 'not available day!!!'] : [true, '','']
}
});
.disable-day{
background-color:red;
}
<link href="https://cdn.bootcss.com/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div class="dates">
<div class="start_date" style="width:50%;margin-right:3%;">
<input readonly="readonly" class="form-control start_date mb-4" type="text" placeholder="start date" id="startdate_datepicker">
</div>
<div class="end_date" style="width:50%;margin-left:3%;">
<input readonly="readonly" class="form-control end_date mb-4" type="text" placeholder="end date" id="enddate_datepicker">
</div>
</div>
答案 1 :(得分:0)
使用.datepicker("option", "minDate", <value of start date>)
设置最早的可选日期。 “ maxDate”将允许您设置最新的可选日期。