简体   繁体   English

在Javascript中更改if语句中变量的值

[英]Changing value of a variable within an if statement in Javascript

I'm having a problem with a piece of code and it's driving me nuts. 我遇到了一段代码问题,这让我感到疯狂。 I've been stuck on this for hours, and the worst part is that I'm assuming it's really simple; 我被困在这几个小时,最糟糕的是我假设它很简单; I just can't figure it out. 我只是想不出来。

I'm trying to make a simple event calendar using Javascript/jQuery. 我正在尝试使用Javascript / jQuery创建一个简单的事件日历。 This is the simplified code that I have: 这是我的简化代码:

var currentMonth = 1;
if (currentMonth == 1) {
    $("#prev-month").click( function() {
        currentMonth = 12;
    });
    $("#next-month").click( function() {
        currentMonth = 2;
    });
}
if ( currentMonth == 2) {
    $("#prev-month").click( function() {
        currentMonth = 1;
    });
    $("#next-month").click( function() {
        currentMonth = 3;
    });
}
if ( currentMonth == 3) {
    $("#prev-month").click( function() {
        currentMonth = 2;
    });
    $("#next-month").click( function() {
        currentMonth = 4;
    });
}
if ( currentMonth == 4) {
    $("#prev-month").click( function() {
        currentMonth = 3;
    });
    $("#next-month").click( function() {
        currentMonth = 5;
    });
}

Now, every time I click the button with the ID "next-month", it is always 2. If I click the button with the ID "prev-month", it is always 12. It never changes. 现在,每次我点击ID为“下个月”的按钮时,它总是为2.如果我点击ID为“prev-month”的按钮,它始终为12.它永远不会改变。 What am I doing wrong? 我究竟做错了什么?

Your script code is only run once. 您的脚本代码只运行一次。 You aren't changing the click handlers after you clicked them, so those handlers will always give the same results. 单击它们后,您不会更改单击处理程序,因此这些处理程序将始终提供相同的结果。

But instead of fixing that it would be simpler to use a single handler for each button and using arithmetic to calculate the next/previous month instead of having 12 handlers which you change at runtime. 但是不是固定,为每个按钮使用单个处理程序并使用算术计算下一个/上个月而不是在运行时更改12个处理程序会更简单。

$("#prev-month").click( function() {
    currentMonth = (currentMonth - 1) || 12;
});
$("#next-month").click( function() {
    currentMonth = currentMonth % 12 + 1;
});

you are using the .click() function wrong. 你正在使用.click()函数错误。 You should make it like this: 你应该这样做:

var currentMonth = 1;

$("#prev-month").click(function() {
    currentMonth--;
    if (currentMonth == 0) {
        currentMonth = 12;
    }
}
$("#next-month").click(function() {
    currentMonth++
    if (currentMonth == 13) {
        currentMonth = 1;
    }
});​

You can use a closure to store your reference and only one click handler (using $(this).is() ): 您可以使用闭包来存储引用,只使用一个单击处理程序(使用$(this).is() ):

<div>
    Current Month: <input type="text" id="current-month"/>
    <button id="prev-month">Previous Month</button>
    <button id="next-month">Next Month</button>
</div>

$(document).ready(function(){
    var currentMonth = 1,
        $currentmonth = $('#current-month');

    $currentmonth.val(currentMonth);

    $("#prev-month, #next-month").click(function() {
        var $this = $(this);

        if ($this.is('#prev-month')) {
            currentMonth = currentMonth - 1;
        } else {
            currentMonth = currentMonth + 1;
        }

        if (currentMonth == 0) {
            currentMonth = 12;
        } else if (currentMonth > 12) {
            currentMonth = 1;
        }

        $currentmonth.val(currentMonth);

        console.log('Current Month: ' + currentMonth);
    });
});

http://jsfiddle.net/pCs2G/ http://jsfiddle.net/pCs2G/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM