开发者

How to change month dynamically in JSP?

In my program, there are two buttons and in the center of the two buttons there is a space for month to display dynamically using JSP, e.g. << current month >>. The << and >> are two buttons.

I need a logical or a programmatic explanation for the following to occur:

  • When I click on the left button the previous month of the current month should display.
  • When I click on the right button the next month of the current month should display.

This should happen dynamically. How can I do tha开发者_JS百科t with help of JSP, JS and/or Ajax?


You can easily do that with jQuery:

HTML:

<a id="Previous" href="#">&lt;&lt;</a>
<span id="CurrentMonth">January</span>
<a id="Next" href="#">&gt;&gt;</a>

Javascript:

var currentMonth = 0;
$(function(){
  var months = ["January", "February", "March", "April", "May", "June",
               "July", "August", "September", "October", "November", "December"];

  $("#Next").click(function() {
    if (currentMonth  < 11) {
      currentMonth++;
      $("#CurrentMonth").text(months[currentMonth]);
    }
  });

  $("#Previous").click(function() {
    if (currentMonth  > 0) {
      currentMonth--;
      $("#CurrentMonth").text(months[currentMonth]);
    }
  });
});

If you want to also inform the server about the current month, you need to create an Ajax service (using for example a servlet).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜