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模式对话框中.
所以这是发生的事情:
似乎日期选择器在部分回发后已经打开时初始化时会感到困惑.我知道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的问题.
根据我提交的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)
onChangeMonthYear
从pageLoad
函数初始化选择器时,使用上面显示的选项:
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)