1

出于某种原因,我的 jQuery 函数在页面加载时运行,而不是在被点击时运行。这是一个例子:http: //jsfiddle.net/KRvvg/

这是代码:

var month = 'February';
var year = 2013;

$(document).ready(function () {
$('#moveMonthForward').click(moveMonthForwardFunction(month, year));

function moveMonthForwardFunction(month, year) {
                    var NextMonth = findNextMonth(month, year);
                    console.log('index.php?Year=' + NextMonth.Year + '&Month=' + NextMonth.Month);
                }

function findNextMonth(CurrentMonth, CurrentYear) {
                    switch (CurrentMonth) {
                        case 'January':
                            var NextMonth = { 'Month': 'February', 'Year': CurrentYear };
                            break;
                        case 'February':
                            var NextMonth = { 'Month': 'March', 'Year': CurrentYear };
                            break;
                        case 'March':
                            var NextMonth = { 'Month': 'April', 'Year': CurrentYear };
                            break;
                        case 'April':
                            var NextMonth = { 'Month': 'May', 'Year': CurrentYear };
                            break;
                        case 'May':
                            var NextMonth = { 'Month': 'June', 'Year': CurrentYear };
                            break;
                        case 'June':
                            var NextMonth = { 'Month': 'July', 'Year': CurrentYear };
                            break;
                        case 'July':
                            var NextMonth = { 'Month': 'August', 'Year': CurrentYear1 };
                            break;
                        case 'August':
                            var NextMonth = { 'Month': 'September', 'Year': CurrentYear };
                            break;
                        case 'September':
                            var NextMonth = { 'Month': 'October', 'Year': CurrentYear };
                            break;
                        case 'October':
                            var NextMonth = { 'Month': 'November', 'Year': CurrentYear };
                            break;
                        case 'November':
                            var NextMonth = { 'Month': 'January', 'Year': CurrentYear };
                            break;
                        case 'December':
                            var NextMonth = { 'Month': 'January', 'Year': CurrentYear + 1 };
                            break;
                    }
                    return NextMonth;
                }
});
4

1 回答 1

7

您不是将回调moveMonthForwardFunction引用传递给 click 方法,而是调用该moveMonthForwardFunction方法并将其返回的值作为 click 处理程序传递。

您应该将函数引用传递给 click 方法,如下所示

$('#moveMonthForward').click(function(){
    moveMonthForwardFunction(month, year)
});
于 2013-08-27T00:36:11.260 回答