我正在使用jquery ui日期选择器来选择请假的开始日期和结束日期。当用户尝试选择离开开始日期时,我将禁用假期以及用户已经应用的休假日期。我从数据库那里得到了那些日子。到目前为止它工作正常。这是我的代码
var unavailableDates=new Array(); //array of dates that should be disabled
//ajax function for getting already applied leave days and holidays
function getUnavailable(){
var rid=$('#rrid').val();
var request=$.ajax({
url: "../Controller/getUnavailableLeaveDays.php",
type: "POST" ,
data:{rid:rid},
dataType: "json"
});
request.done(function(data){
unavailableDates=data;
});
request.fail(function(jgXHR,textStatus){
alert ("Request failed:" + textStatus);
return false;
});
}
function unavailable(date) {
dmy = date.getFullYear()+ "-" + (date.getMonth()+1) + "-" +date.getDate() ;
if ($.inArray(dmy, unavailableDates) < 0) {
return [true,"","select"];
} else {
return [false,"","All ready Apllied for Leave or a holiday"];
}
}
$(function() {
$( "#leave_commence" ).datepicker({
defaultDate: "+1w",
beforeShowDay: unavailable,
dateFormat:"yy-mm-dd" ,
numberOfMonths: 2,
onClose: function( selectedDate ) {
$( "#leave_ends" ).datepicker( "option", "minDate", selectedDate );
}
});
$( "#leave_ends" ).datepicker({
defaultDate: "+1w",
beforeShowDay: unavailable,
dateFormat:"yy-mm-dd" ,
numberOfMonths: 2,
onClose: function( selectedDate ) {
$( "#leave_commence" ).datepicker( "option", "maxDate", selectedDate );
}
});
});
现在我想从日期选择器中禁用周末。我可以在beforeShowDay中使用noWeekends()方法。但是我已经为beforeShowDay定义了一个函数。所以我试图在该函数中使用noWeekend()。但它是不工作。我现在该怎么办?请帮助我。
答案 0 :(得分:0)
不要将字符串与日期进行比较,请按照here的说明使用valueOf()
并查看以下代码:
function unavailable(date) {
dmy = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
var found = false;
for (var i in unavailableDates) {
if (dmy.valueOf() == unavailableDates[i].valueOf()) {
found = true;
}
}
if (!found) {
return [true, "", "select"];
} else {
return [false, "red2", "no"];
}
}
CSS中的可以添加:
.red2 {
background-color:red
}