简体   繁体   English

创建上一个功能以查看日历中的上个月

[英]Create a prev function to view the previous month in calendar

I'm trying to create a calendar, with function of prev and next, As of now the calendar is working find It now display the current month and year. 我正在尝试创建一个具有上一个和下一个功能的日历,到目前为止,该日历正在工作中,它可以显示当前月份和年份。 The next function is also working except for the prev function which display the previous month. 除显示上个月的上一个功能外,下一个功能也起作用。 No problem in next function, but on the prev function it does not produce the correct month after reaching, 0. I suspected that it does not meet the correct condition or the decrement produced negative or something? 下一个函数没问题,但是在上一个函数上,到达零后不会产生正确的月份,我怀疑它不符合正确的条件或减量产生了负数或某些东西? any idea on this? 有什么想法吗?

  var d = new Date();
  var m = d.getMonth(); 
  var y = d.getFullYear(); 
$(document).on('click', '#next', function(){
          var str = ++m;
          var str1 = y;
          if(str > 11)
          {
              m = 0;
              str1 = y++;
          }
        /* some more codes */ 
  });

$(document).on('click', '#prev', function(){             
          m -= 1;
          var str = m;
          var str1 = y;              
          if(str < 1)
          {
              m = 11;
              str1 = --y;
          }    
         /* some more codes */  
          var url = calendar_vars.plugin_url + "?month=" + str +"&"+"year="+str1;
          xmlhttp.open("GET",url,true);
          xmlhttp.send(); 
  });

Try this : 尝试这个 :

$(document).on('click', '#next', function(){
   m++;
   if(m > 11) {
      m = 0;
      y++;
   }
   /* some more codes */ 
});
$(document).on('click', '#prev', function(){
          m--;             
          if(m < 0)
          {
              m = 11;
              y--;
          }    
         /* some more codes */  
          var url = calendar_vars.plugin_url + "?month=" + m +"&"+"year="+y;
          xmlhttp.open("GET",url,true);
          xmlhttp.send(); 
    });

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

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