我正在为我的网站使用datepicker。我选择了几天不接受订单。当我禁用了一天,实际上是那天,即使我不允许,也会选择默认日期作为当天。
让我解释一下:
我希望发生的是默认日期设置为下一个可用日期,在我的示例中将是3月18日。
有没有办法将默认日期设置为禁用日期后的下一个可用日期?
提前致谢。
到目前为止,我的JS代码如下:
<script type="text/javascript">
jQuery.noConflict();
jQuery(document).ready(function($) {
$("#input_1_16").datepicker({ beforeShowDay: nationalDays, minDate: 0, maxDate: "+4m"})
natDays = [
[1, 26],
[2, 6],
[3, 15,17],
[4, 27],
[5, 15,25],
[6, 6],
[7,19],
[8,27],
[9,],
[10,],
[11,],
[12,23,24,25,30,31]
];
function nationalDays(date) {
for (i = 0; i < natDays.length; i++) {
if (date.getMonth() == natDays[i][0] - 1
&& date.getDate() == natDays[i][1]) {
return [false, natDays[i][2] + '_day'];
}
}
return [true, ''];
}
});
</script>
答案 0 :(得分:1)
我的好友能够为我解答这个问题:代码如下,因为这里有一个工作示例http://jsfiddle.net/NHdEX/8/
natDays = [
[1,26],
[2,6],
[3,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31],
[4,27],
[5,15,25],
[6,6],
[7,19],
[8,27],
[9],
[10],
[11],
[12,23,24,25,30,31]
];
function getMinDate() {
var now = new Date();
var monthnumber = now.getMonth();
var monthday = now.getDate();
var year = now.getFullYear();
//check if today is a holiday.
//by default do not skip any dates, allow user to select today.
var dayOffset = 0;
var date;
var currentDay = monthday;
var currentMonth = monthnumber;
for(var i = 0; i < natDays.length; i++){
date = natDays[i];
//check month
if(date[0] == currentMonth+1){
for(var j=1;j<date.length;j++){
currentDay == 0;
while(date[j] == currentDay){
dayOffset++;
currentDay++;
}
}
}
}
//calculate the new date.
now.setDate(now.getDate() + dayOffset);
return (now.getMonth() + 1) + '/' + now.getDate() + '/' + now.getFullYear();
}
function nationalDays(date) {
for (i = 0; i < natDays.length; i++) {
if (date.getMonth() == natDays[i][0] - 1
&& date.getDate() == natDays[i][1]) {
return [false, natDays[i][2] + '_day'];
}
}
return [true, ''];
}
$("#input_1_16").val(getMinDate());
$("#input_1_16").datepicker({ beforeShowDay: nationalDays, minDate: getMinDate(), maxDate: "+4m"});
答案 1 :(得分:0)
// Getting the first good day
var default = new Date();
while(nationalDays(default)[0]){
default = new Date().setDate(default.getDate()+1);
}
// Creating by giving the first good day as default
$("#input_1_16").datepicker({defaultDate: default beforeShowDay: nationalDays, minDate: 0, maxDate: "+4m"});