Javascript如果那么else语句

时间:2014-02-11 17:11:51

标签: javascript if-statement

我试图使用If then else语句调用警报中的变量。

我正在页面加载时创建全局变量currentmonth。我测试了一个值正在加载到currentmonth并且它正在工作。

我还在页面加载时创建全局变量(datejanuary到datedecember)。我也测试了这些值,他们正在工作。

基本上我想要做的是获取当前月份并将其与数组中的值进行比较,并在警报中显示消息。例如,1月份的消息将是“迷你高尔夫 - 高尔夫可能是迷你高尔夫,竞争对手会很大。对胜利者来说是战利品!” 2月的消息将是“徒步旅行 - 这不是关于目的地,而是关于旅程。”

以下是我为此创建的功能,但我无法使其工作。我错过了什么?

    <script>
    function dateMonth()
    {
    var currentdate = "";
    if (currentmonth == "January")
    {
    currentdate = datejanuary;
    }
    else if (currentmonth == "February")
    {
    currentdate = datefebruary;
    }
    else if (currentmonth == "March")
    {
    currentdate = datemarch;
    }
    else if (currentmonth == "April")
    {
    currentdate = dateapril;
    }
    else if (currentmonth == "May")
    {
    currentdate = datemay;
     }
     else if (currentmonth == "June")
     {
     currentdate = datejune;
     }
     else if (currentmonth == "July")
     {
     currentdate = datejuly;
     }
     else if (currentmonth == "August")
     {
      currentdate = dateaugust;
      }
      else if (currentmonth == "September")
     {
     currentdate = dateseptember;
      }
      else if (currentmonth == "October")
      {
      currentdate = dateoctober;
      }
      else if (currentmonth == "November")
      {
      currentdate = datenovember;
      }
      else
      {
      currentdate = datedecember;
      }
      {
      vex.dialog.alert(currentdate);   
      }
      </script>

2 个答案:

答案 0 :(得分:4)

简单地说:

var messages = [
   "Mini Golf – the golf may be mini, the competition will be big. To the winner go the ..",
   "Hike – it’s not about the destination, it’s about the journey.",
   "..."
];

alert( messages[new Date().getMonth()] );

(1月getMonth() == 0

答案 1 :(得分:0)

左右如果真的需要使用if和else

<script>
    function dateMonth()
    {
        var currentdate = "";
        var currentmonth = new Date().getMonth()
        if (currentmonth == 1)
        {
            currentdate = datejanuary; //Your text
        }
        else if (currentmonth == 2)
        {
            currentdate = datefebruary;
        }
        else if (currentmonth == 3)
        {
            currentdate = datemarch;
        }
        else if (currentmonth == 4)
        {
            currentdate = dateapril;
        }
        else if (currentmonth == 5)
        {
            currentdate = datemay;
        }
        else if (currentmonth == 6)
        {
            currentdate = datejune;
        }
        else if (currentmonth == 7)
        {
            currentdate = datejuly;
        }
        else if (currentmonth == 8)
        {
            currentdate = dateaugust;
        }
        else if (currentmonth == 9)
        {
            currentdate = dateseptember;
        }
        else if (currentmonth == 10)
        {
            currentdate = dateoctober;
        }
        else if (currentmonth == 11
        {
            currentdate = datenovember;
        }
        else
        {
            currentdate = datedecember;
        }
        vex.dialog.alert(currentdate);   
    }
</script>