在ASP.NET部分回发期间打开时的jQueryUI datepicker行为:年份变为1899或1900

Tom*_*ing 5 asp.net jquery updatepanel jquery-ui jquery-ui-datepicker

我有一个非常有趣的bug,它有一个jQueryUI datepicker和一个UpdatePanel,其中datepicker的选择日期约为100年.jQuery是版本1.6.2,jQueryUI是版本1.8.14.这是粗略的布局:

<asp:UpdatePanel ID="myUpdatePanel" runat="server">
  <ContentTemplate>
    <asp:DropDownList ID="myDdl" runat="server" AutoPostBack="true">
      <asp:ListItem Value="foo" Text="Foo" />
      <asp:ListItem Value="bar" Text="Bar" />
    </asp:DropDownList>
    <asp:TextBox ID="myText" runat="server" CssClass="dateText" />
  </ContentTemplate
</asp:UpdatePanel>
<script type='text/javscript'>
  $(document).ready(function()
  {
     $('.dateText').datepicker({ changeYear: true, showButtonPanel: true, yearRange: '-2:+2' });
  }

  function pageLoad(sender, args)
  {
    if (args._isPartialLoad == true)
    {
      $('.dateText').datepicker({ changeYear: true, showButtonPanel: true, yearRange: '-2:+2' });
    }
  }
</script>
Run Code Online (Sandbox Code Playgroud)

所以我有一个UpdatePanel,它包含一个下拉列表,当它被更改时会导致回发,以及一个文本框.我有jQueryUI将文本框设置为datepicker.请注意,所有这些都包含在jQueryUI模式对话框中.

所以这是发生的事情:

  1. 对话框打开时,下拉列表是默认焦点.按键盘上的键将更改下拉列表中的选择,但不会启动回发.
  2. 使用键盘更改下拉列表的值后,单击文本框.部分回发开始(因为下拉失去焦点)和jQueryUI DatePicker出现显示当前月份和年份(2012年2月撰写).
  3. 在我们有机会对datepicker做任何事情之前,部分回发可能会结束.
  4. 回发完成后,单击日期选择器上的前进/后退月份按钮之一.如果您向后按,则会转到2010年11月.如果您向前按,则会转到2010年1月.
  5. 使用按钮更改月份后,单击日历中的某一天.datepicker关闭并在文本框中输入日期.如果在步骤4中向后按,则日期为1899年11月.如果在步骤4中向前按,则日期为1900年1月.

似乎日期选择器在部分回发后已经打开时初始化时会感到困惑.我知道datepicker设置不能在部分回发中存活,我需要在pageLoad函数中重新初始化它.

我可以通过$('.ui-datepicker-title').length在设置日期选择器时检查是否大于零来判断是否发生了这种情况pageLoad.但我不知道该怎么做.在验证它正在发生之后,我pageLoad在设置datepicker之前尝试了以下内容:

  • $('.dateText').unbind();
  • $('.dateText').datepicker('destroy');
  • $('#ui-datepicker-div').remove();

它既可以做同样的事情,也可以自行关闭,不会再出现了.

我怎样才能解决这个问题?

更新:我尝试使用jQueryUI 1.8.18和jQuery 1.7.1(编写本文时的最新版本),问题仍然存在.

更新2:我设法通过在onChangeMonthYear初始化datepicker 的选项中提供函数来解决此问题pageLoad.如果该事件的年份参数是1899或1900,我将日期选择器的日期设置为从现在开始一个月或向前(分别).这设置了我不想要的文本框内容,因此我将文本框的值设置为''.最终结果是datepicker正常运行.

我仍然希望更好地理解这整个问题,所以我知道如果jQueryUI中的错误是我应该提交的,或者我是否应该在其他地方做一些不同的事情.

更新3:仍然在ASP.NET 4,jQuery 1.7.2和jQueryUI 1.8.21中发生.我在一个其他空白页面上测试了它,其设置就像这里描述的那样,并且看到了相同的行为.我称之为jQueryUI错误.它在这里提交.

更新4:我在这个jsfiddle中重现了没有ASP.NET的问题.

Tom*_*ing 5

根据我提交的jQueryUI错误的评论,这不是一个应该真正发生的情况.但我认为它会发生,所以我将在此发布我的解决方法以供将来参考.

给定ID输入myInput:

var changingDate = false;
$('#myInput').datepicker({onChangeMonthYear:function(year, month, inst)
  {
    if (changingDate == false)
    {
      changingDate = true; //I set the date later which would call this
          //and cause infinite recursion, so use this flag to stop that
      if (year == 1899 || year == 1900)
      {
        var now = new Date(); //what the picker would have had selected
            //before clicking the forward/backward month button
        if (year == 1899) //I clicked backward
        {
           now.setMonth(new.getMonth() - 1);
        }
        else if (year == 1900) //I clicked forward
        {
           now.setMonth(now.getMonth() + 1);
        }
        $(this).datepicker('setDate', now);
      }
      changingDate = false;
    }
    }
  });
Run Code Online (Sandbox Code Playgroud)

onChangeMonthYearpageLoad函数初始化选择器时,使用上面显示的选项:

function pageLoad(sender, args)
{
  if (args._isPartialLoad == true) //called because of UpdatePanel refresh
  {
    //set up datepicker as shown above
  }
}
Run Code Online (Sandbox Code Playgroud)