2

我有 2 个我编写的函数(大部分是修改的),我需要将它们组合成一个函数。第一个函数在用户选择部门时调用,它执行回调以查找日期并将它们添加到数组中。第二个函数在 jQuery 日期选择器中阻止天数。我已经让被封锁的日子正常工作,我只是不知道如何结合这两个功能。我尝试了很多不同的方法,但似乎没有任何效果。

// load calendar buttons
$(document).ready(function(){
$("#Return_Date").datepicker( { beforeShowDay: nonWorkingDates, showOn: "both",    numberOfMonths: 2, minDate: 0, maxDate: "364D", gotoCurrent: true, buttonImageOnly: true }   );
$("#Depart_Date").datepicker( { beforeShowDay: nonWorkingDates, showOn: "both", numberOfMonths: 2, minDate: 0, maxDate: "364D", gotoCurrent: true, buttonImageOnly: true    }   );
});

// load blocked days - this loads properly into the date pickers above
function nonWorkingDates(date){
// create an array for closedDates
var closedDates = [[3,22,2012], [3,25,2012], [4,15,2012], [4,24,2012], [4,25,2012]];

//loop through the list of closed Dates
for (i = 0; i < closedDates.length; i++) {      
    // if the date is found set it as disabled. January is 0, February 1, etc so a -1 is needed on the month value
    if (date.getMonth() == closedDates[i][0] - 1 && date.getDate() == closedDates[i][1] &&  date.getFullYear() == closedDates[i][2]) {
        return [false];
    }
}
return [true];
};

//load in closed dates i need the BlockedTravelDays array to pull into the date pickers. the array here loads fine
$(document).ready(function NoTravelDays() {
$("#TripApprovalDepartment").change(function() {
    var value = $.trim($("#TripApprovalDepartment").val());
    if (value.length > 0) {
        $.getJSON("../approval.cfc?method=getBlockedDays&returnFormat=json", {DeptID:value}, function(res,code) {
            // create an Array
            var BlockedTravelDays = [];
            for (var i = 0; i < res.DATA.length; i++) { 
                // store the travel days in the array
                BlockedTravelDays.push(res.DATA[i][2]);
            }; // end for loop
        }); // end JSON
    } // end if
});
});
4

1 回答 1

2

一个简单的解决方案就是从第三个函数调用这两个函数。这没有某些人可能需要的灵活性,但似乎您不需要同样的灵活性。所以,我们会得到这样的东西:

$(document).ready(function()
{
    function1();
    function2();
}

看到您已经在使用匿名函数,该函数可以只调用另一个函数,从而为您提供:

$(document).ready(function(){
    $("#Return_Date").datepicker( { beforeShowDay: nonWorkingDates, showOn: "both",    numberOfMonths: 2, minDate: 0, maxDate: "364D", gotoCurrent: true, buttonImageOnly: true }   );
    $("#Depart_Date").datepicker( { beforeShowDay: nonWorkingDates, showOn: "both", numberOfMonths: 2, minDate: 0, maxDate: "364D", gotoCurrent: true, buttonImageOnly: true    }   );

    NoTravelDays();
});
于 2012-04-20T20:08:07.393 回答