jQuery datepicker - 如何使用API​​更改月份?

时间:2012-04-25 13:30:10

标签: jquery datepicker

我有一个jquery datepicker设置。我已经定制了这个,所以在datepicker div中,我创建了一个自定义菜单,有几个月像这样:

    <div class="custom_menu">
    <div class="month">January 2012</div>
    <div class="month">February 2012</div>
    <div class="month">March 2012</div>
    </div>

我要做的是当用户点击一个月时,我希望datepicker更新该月的视图,例如一些伪代码

$('.month').live('click', function(){

var newmonth = $(this).text(); //get the month in a suitable format

$('#current_datpicker').update('month', newmonth); //update the datepicker to show this month

});

我可以做这样的事情,看看API文档没有显示任何内容......

修改

我面临的问题是当我在页面上有多个datepicker实例时,如何获取#current_datepicker(即正确的datepicker id)?当前的datepicker触发器输入id是否存储在某个地方?

2 个答案:

答案 0 :(得分:2)

$('#current_datpicker').datepicker("setDate", newmonth + 'm');

source

虽然$(this).text()不会只提供月份,但您需要删除年份:

var newmonth = $(this).text().split(" ")[0];

答案 1 :(得分:1)

您将不得不使用.datepicker( "setDate" , date )方法。我建议用户点击一个月时,将日期设置为该月的第一天。

您可以使用$.data() jQuery机制来存储每个div.的日期信息。例如,您可以按照这些行$('#').data('data', new Date('1/1/2012')进行操作。

查看API