代码之家  ›  专栏  ›  技术社区  ›  user61526

jquery datepicker在按钮栏上有条件地禁用今天

  •  0
  • user61526  · 技术社区  · 7 年前

    我有一个jquery日期选择器,其中日历中只有某些日期是可分割的。按钮栏上的“今天”按钮的功能已被覆盖,以在字段中填充今天的日期。现在,如果今天不是用户选择的有效日期,我将尝试禁用按钮栏上的“今天”按钮。我试图通过将ui datepicker当前类更改为包含禁用的ui状态而不是默认的ui状态来实现这一点。但我似乎无法改变课程。我已尝试在PreforeShow中添加逻辑:

    $(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 ); }
      });
    

    以及在该块下面添加逻辑:

    $(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");
    

    但是,两者都没有更改与当前ui日期选择器的按钮关联的类。有谁能告诉我如何使用那个按钮吗?

    非常感谢。

    1 回复  |  直到 7 年前
        1
  •  0
  •   tata.leona    7 年前

    在执行beforeShow之后使用超时修改按钮:

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

    参见示例 here

    你还必须处理 onChangeMonthYear 事件