jQuery 日期选择器在 .(“show”) 之后失去焦点

发布于 2024-12-06 23:14:56 字数 748 浏览 1 评论 0原文

我正在使用 jQuery UI 的范围日期选择器。选择“起始日期”时,我想自动打开“截止日期”日期选择器。所以,我调用 .datepicker("show")。 “迄今为止”选择器显示一秒钟,然后立即消失。令人惊讶的是,如果我转到另一个应用程序,然后返回并关注浏览器窗口,则会显示“最新”选择器。 我还尝试添加 $('#toDate').focus();但这没有帮助。

$( ".fromDatePicker" ).datepicker({
    defaultDate: "+1w",
    dateFormat: 'dd/mm/yy',
    altFormat: 'yymmdd',
    altField: "#fromDateFormatted",
    numberOfMonths: 2,
    showOn: "both",
    buttonImage: "images/calender_icon_a1.jpg", 
    buttonText: "open calendar",
    buttonImageOnly: true,
    onSelect: function( selectedDate ) {
        $('#toDate').datepicker( "option", "minDate", selectedDate );
        $('#toDate').datepicker("show");
        //$('#toDate').focus();  //commented cause it's not working
    }
});

I'm using the range date-picker of jQuery UI. When selecting the "from date", I want to open automatically the "to date" date-picker. So, I call the .datepicker("show"). The "to date" picker is showing for a second and immediately fade away. Surprisingly, if I go to another application and then come back and focus on the browser window, the "to date" picker is shown.
I also tried to add the $('#toDate').focus(); but it didn't help.

$( ".fromDatePicker" ).datepicker({
    defaultDate: "+1w",
    dateFormat: 'dd/mm/yy',
    altFormat: 'yymmdd',
    altField: "#fromDateFormatted",
    numberOfMonths: 2,
    showOn: "both",
    buttonImage: "images/calender_icon_a1.jpg", 
    buttonText: "open calendar",
    buttonImageOnly: true,
    onSelect: function( selectedDate ) {
        $('#toDate').datepicker( "option", "minDate", selectedDate );
        $('#toDate').datepicker("show");
        //$('#toDate').focus();  //commented cause it's not working
    }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

送你一个梦 2024-12-13 23:14:56

出现闪烁的原因是因为 show 会在 minDate 完成之前被调用。在显示选择器之前触发 beforeShowDay 事件时,这会使日期选择器感到困惑。

一种有点棘手的解决方法是延迟调用以显示日期选择器。例如,类似以下内容就可以:

onSelect: function( selectedDate ) {
    $('#toDate').datepicker( "option", "minDate", selectedDate );
    setTimeout(function() { $('#toDate').datepicker("show") }, 50);
}

查看实际操作:http://jsfiddle.net/william /PVuTC/2/

The reason for the flashing appearance is because show would be called before minDate finishes. This would confuse datepicker when triggering beforeShowDay event just before showing the picker.

One somewhat hacky workaround is to delay the call to show the datepicker. For example, something like the following would work:

onSelect: function( selectedDate ) {
    $('#toDate').datepicker( "option", "minDate", selectedDate );
    setTimeout(function() { $('#toDate').datepicker("show") }, 50);
}

See this in action: http://jsfiddle.net/william/PVuTC/2/.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文