【问题标题】:jQuery datepicker - how to change the month using the API?jQuery datepicker - 如何使用 API 更改月份?
【发布时间】:2012-04-25 13:30:10
【问题描述】:

我有一个 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>

我想要做的是当用户点击一个月份时,我希望日期选择器用那个月份更新它的视图,例如一些伪代码

$('.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 文档没有显示任何内容...

编辑

我面临的问题是当我在页面上有超过 1 个日期选择器实例时如何获取 #current_datepicker(即正确的日期选择器 ID)?当前的日期选择器触发器输入 ID 是否存储在某处?

【问题讨论】:

    标签: jquery datepicker


    【解决方案1】:
    $('#current_datpicker').datepicker("setDate", newmonth + 'm');
    

    source

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

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

    【讨论】:

    • 如果我在我的页面上使用超过 1 个 datepicker 实例,如何获得正确的 datepicker ID?
    • 我的意思是通过 API - 因为$(.month).live('click', function...) 函数将无法判断将更新发送到哪里
    • @Ash。您可以给 datepicker 一个“匹配” div id 的 id。 &lt;div id="firstDiv&gt;&lt;input id="firstDatepicker"&gt;
    • @Ash。你知道 datepicker 只是一个输入,对吧?无论如何,您可以使用代码打开日期选择器。像这样:$('#current_datpicker').datepicker( "show" )
    【解决方案2】:

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

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

    查看API

    【讨论】:

      猜你喜欢
      • 2013-11-15
      • 1970-01-01
      • 2011-11-23
      • 1970-01-01
      • 1970-01-01
      • 2021-03-07
      • 2018-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多