jQuery UI Datepicker预选不是星期天的日期 [英] Jquery ui datepicker preselect date that is not a sunday
问题描述
我修改了jQuery ui datepicker,以预先选择从现在起2天的日期,并且我还禁用了datepicker中的Sundays.但是,如果星期日距现在2天,则输入字段仍将用星期日预填充.我该如何预防?
I have modified the jquery ui datepicker to pre-select the day that is 2 days from now and I have also disabled Sundays in the datepicker. However, the input field is still being pre-filled with the Sunday if the Sunday is 2 days from now. How do I prevent this?
$(function() {
$("#delivery-date").datepicker({
minDate: +2,
maxDate: '+2M',
dateFormat: "DD, d MM yy",
showOn: "both",
buttonText: "Change",
beforeShowDay: function(date) {
var day = date.getDay();
return [(day != 0), ''];
}
}).datepicker("setDate", "0");
});
推荐答案
假设您将切换到第二天,星期一,则可以使用条件(三元)运算符.
Assuming you would switch to the Next day, Monday, you can use conditional (ternary) operator.
这里是一个例子.
$(function() {
$("#delivery-date").datepicker({
minDate: new Date().getDay() + 2 == 7 ? 3 : 2,
maxDate: '+2M',
dateFormat: "DD, d MM yy",
showOn: "both",
buttonText: "Change",
beforeShowDay: function(date) {
var day = date.getDay();
return [(day != 0), ''];
}
}).datepicker("setDate", "0");
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<p>Date: <input type="text" id="delivery-date"></p>
如果今天是星期五(5),则加上2天,则将其设为下周的星期日(7,或JS:0).所以我们可以检查一下.
If today is Friday (5), adding 2 days, would make it Sunday of the next week (7, or in JS: 0). So we can check for that.
通常我们可以创建一个函数:
Normally we might make a function:
function pickDay(n){
var d = new Date().getDay(); // Returns the day of the week: 5
if(d + n == 7){
return n + 1;
}
return n;
}
然后像这样使用它:
minDate: pickDay(2),
我对此进行了测试,但它不受欢迎,似乎存在一些计时问题.
I tested this and it's not liked, there seems to be some timing issue.
这篇关于jQuery UI Datepicker预选不是星期天的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!