我已经使用beforeShow和beforeShowDay回调实现了禁用日期的代码
以下代码用于将日历绑定到输入
$("#start_on").datepicker({
onSelect: function(date) {
date_check(date);
}, minDate: 0, dateFormat: "yy-mm-dd",
beforeShow: get_booked,
beforeShowDay: disable_if_not_available
});
是的,这是将日历绑定到元素
回调beforeShow
function get_booked(inpput, inst) {
var D = new Date();
var month = (inst.selectedMonth == 0 ? D.getMonth() : inst.selectedMonth) + 1;
var year = (inst.selectedYear == 0 ? D.getYear() : inst.selectedYear);
dis_date = new Array();
$.ajax({
url: BASE_URL + "contest/get_booked",
data: {year: year, month: month, id:<?php echo (isset($contest_data->id)) ? $contest_data->id : 0; ?>},
dataType: "json", type: "POST",
success: function(data) {
for (var d in data) {
var D = data[d];
var start = D.start_on;
var d = start.split("-");
var sD = new Date(d[0], d[1] - 1, d[2]);
var end = D.end_on;
var d = end.split("-");
var eD = new Date(d[0], d[1] - 1, d[2]);
while (sD <= eD) {
dis_date.push(sD.getFullYear() + "-" + (sD.getMonth() + 1) + "-" + sD.getDate());
sD.setDate(sD.getDate() + 1);
}
}
}
});
}
回调beforeShowDay
function disable_if_not_available(date) {
var y = date.getFullYear(), m = date.getMonth() + 1, d = date.getDate();
return [dis_date.indexOf(y + "-" + m + "-" + d) == -1];
}
问题是disable_if_not_available
在dis_date
填充get_booked
之前被调用。目前disable_if_not_available
返回始终为真,因为dis_date
为空,所以我想做的是仅在我收到disable_if_not_available
get_booked
请询问是否有任何疑问
答案 0 :(得分:0)
这是因为在ajax调用返回任何值之前,datepicker的整个初始化结束。
你可以做的是启动ajax调用,当调用结束时,调用datepicker的初始化。
示例:
...
...
$.ajax({
url: BASE_URL + "contest/get_booked",
data: {year: year, month: month, id:<?php echo (isset($contest_data->id)) ? $contest_data->id : 0; ?>},
dataType: "json",
type: "POST",
success: function(data) {
for (var d in data) {
var D = data[d];
var start = D.start_on;
var d = start.split("-");
var sD = new Date(d[0], d[1] - 1, d[2]);
var end = D.end_on;
var d = end.split("-");
var eD = new Date(d[0], d[1] - 1, d[2]);
while (sD <= eD) {
dis_date.push(sD.getFullYear() + "-" + (sD.getMonth() + 1) + "-" + sD.getDate());
sD.setDate(sD.getDate() + 1);
}
}
//CALL YOUR DatePicker Initialization
initDatePicker();
}
});
...
...
function initDatePicker(){
$("#start_on").datepicker({/*Initialization values*/});
}
答案 1 :(得分:0)
简单的解决方案。只需使用&#39; async:false&#39;在你的ajax电话中。
示例:
...
...
$就({
url:BASE_URL +&#34;竞赛/ get_booked&#34;,
data:{year:year,month:month,id:id))? $ contest_data-&gt; id:0; ?&GT;},
dataType:&#34; json&#34;,
键入:&#34; POST&#34;,
异步:假的,
成功:功能(数据){
...
...