简体   繁体   English

jQuery datepicker有条件禁用按钮栏上的Today

[英]jquery datepicker conditionally disable Today on button bar

I have a jquery datepicker where only certain dates are sectable in the calendar. 我有一个jQuery datepicker,其中日历中只有某些日期是可分割的。 The functionality of the today button on the button bar has been overridden to populate today's date in the field. 按钮栏上的“今天”按钮的功能已被覆盖,以在该字段中填充今天的日期。 Now I am trying to disable the today button on the button bar if today is not a valid date for the user to choose. 现在,如果今天不是用户可以选择的有效日期,我将尝试禁用按钮栏上的“今天”按钮。 I am trying to do this by changing the ui-datepicker-current class to contain ui-state-disabled rather than ui-state-default. 我正在尝试通过将ui-datepicker-current类更改为包含ui-state-disabled而不是ui-state-default来做到这一点。 I cannot seem to change the classes though. 我似乎无法更改类。 I have tried adding logic to beforeShow: 我试过向beforeShow添加逻辑:

$(document).ready( function() {
  $( ".MyForm-date" ).datepicker( {
    changeYear: true,
    dateFormat: "dd M yy",
    yearRange: "-20:+10",
    showButtonPanel: true,
    beforeShow : function( input, instance ) { 
        instance.dpDiv.find(".ui-datepicker-current").removeClass("ui-state-default").addClass("ui-state-disabled");
        return initGoodReportDates( input, instance ); },
    onChangeMonthYear : function( year, month, instance ) { return  refreshGoodReportDates(year, month, instance); },
    beforeShowDay : function( date ) { return isGoodReportDate( date ); },
    onClose : function(){ checkDate( this ); }
  });

as well as adding logic below this block: 以及在此块下面添加逻辑:

$(document).ready( function() {
  $( ".MyForm-date" ).datepicker( {
    changeYear: true,
    dateFormat: "dd M yy",
    yearRange: "-20:+10",
    showButtonPanel: true,
    beforeShow : function( input, instance ) { 
        return initGoodReportDates( input, instance ); },
    onChangeMonthYear : function( year, month, instance ) { return refreshGoodReportDates(year, month, instance); },
    beforeShowDay : function( date ) { return isGoodReportDate( date ); },
    onClose : function(){ checkDate( this ); }
  });

$(".MyForm-date").datepicker("widget").find("ui-datepicker-current").removeClass("ui-state-default").addClass("ui-state-disabled");

however neither have changed the classes associated with button for ui-datepicker-current. 但是,都没有更改与ui-datepicker-current的按钮关联的类。 Can anyone please tell me how I can access that button? 谁能告诉我如何访问该按钮?

Thank you 谢谢

Use a timeout to modify button just after beforeShow is executed: 在执行show之前立即使用超时来修改按钮:

    $('.foo').datepicker({
        beforeShow: function(input, inst) {
            setTimeout(function() { 
              //...
            }, 0);   
        }
    })

See example here 在这里查看示例

You also have to handle onChangeMonthYear event. 您还必须处理onChangeMonthYear事件。

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

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