1

我正在使用 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()。但它不起作用。我现在该怎么办?请帮助我。

4

1 回答 1

0

不要将字符串与日期进行比较,请按照此处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
}

jsFiddle

于 2013-06-15T21:15:48.737 回答