jQuery结合两个用于更改的函数

时间:2012-04-20 20:01:29

标签: jquery

我有两个我写的(大部分是修改过的)函数,我需要将它们组合成一个函数。当用户选择一个部门时会调用第一个函数,它会进行回调以查找天数并将它们添加到数组中。第二个函数在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
});
});

1 个答案:

答案 0 :(得分: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();
});