我遇到一个问题,jQuery UI 的 Datepicker 导致 Internet Explorer(版本 8 和 9,也许其他版本)有时(但并非总是)崩溃。 IE 在页面加载时或摆弄日期选择器字段后大约有 25% 的时间崩溃。这可能是 Windows 7 所特有的,也可能不是。我可以使用从 1.6.2 到 1.7.1 的各种不同版本的 jQuery 重现该问题。
<script>
$(function() {
$('#someDate').datepicker();
});
</script>
<input type="text" name="someDate" id="someDate" value="">
通过导航到 jsfiddle 页面,然后开始玩弄(刷新、选择日期、跳出字段等),我可以很容易地让 IE 崩溃。
当 IE 崩溃时,会显示一个对话框:“Internet Explorer 已停止工作 - 出现问题导致程序停止正常工作。Windows 将关闭该程序并通知您是否有可用的解决方案。”事件日志显示事件 ID 1000 的应用程序错误。
有什么建议吗?
~~~~~~~~~
更新:这个问题确实得到了解决,但我犹豫是否将其标记为已回答或提供真正的答案,因为我不明白为了使其发挥作用而进行了哪些更改。所有问题的发生都发生在 IT 人员分配的虚拟机上。 IT 人员更改了虚拟机上的某些内容(可能是 Windows 设置?),问题得到了解决。
最佳答案
如果我在初始化 DatePicker 元素时执行类似的操作,这是互联网上有人建议的:
$('.DatePicker').datepicker({
onChangeMonthYear: function(year, month, inst) {
var now = new Date(this.value);
if (now) {
var max = new Date(year, month, 0).getDate();
var day = now.getDate() > max ? max : now.getDate();
var newDate = new Date(year, month-1, day);
inst.input.datepicker('setDate', newDate);
}
}
});
(当然,我已在所需的输入元素处附加了 class=DatePicker。)
然后 IE 在更改日期时总是崩溃。
如果我省略 onChangeMonthYear 处理程序或只是注释掉 inst.input.datepicker ... 行,问题就会消失(当然在这种情况下所需的功能也会消失)。
所以我又做了一些实验,发现问题出在第一行:
var now = new Date(this.value);
这里我们尝试根据输入字段值构造一个日期变量。看来 IE 在本地化格式时不会构造(就像我的匈牙利语),因此结果将为 NaN。看起来这才是真正的问题。无论如何,Chrome 和 Firefox 都能正常运行对我来说很奇怪。
所以我将第一行更改为:
var now = inst.input.datepicker('getDate');
并且不再发生崩溃。
我在底线发现了另一个奇怪的行为:DatePicker 可见一个小矩形。当鼠标悬停时,这也会导致一些错误。但我找到了一个解决方案,在我的主模板中的 jquery.ui.css 之后添加一小段 css:
#ui-datepicker-div
{
display: none;
}
所以我很高兴它能按照我的要求在 IE6、Chrome、Firefox 中运行。
这是我的日期选择器的最终初始化代码:
$('.DatePicker').datepicker({
onChangeMonthYear: function(year, month, inst) {
var now = inst.input.datepicker('getDate');
if (now) {
var max = new Date(year, month, 0).getDate();
var day = now.getDate() > max ? max : now.getDate();
var newDate = new Date(year, month-1, day);
inst.input.datepicker('setDate', newDate);
}
}
});
还有我的日期时间选择器:
$('.DateTimePicker').datetimepicker({
onChangeMonthYear: function(year, month, inst) {
var now = inst.input.datepicker('getDate');
if (now) {
var max = new Date(year, month, 0).getDate();
var day = now.getDate() > max ? max : now.getDate();
var newDate = new Date(year, month-1, day,
now.getHours(), now.getMinutes(), now.getSeconds());
inst.input.datepicker('setDate', newDate);
}
}
});
我正在使用 jQuery 1.7.1 和 jquery.ui.1.8.16
米克洛斯
关于jQuery UI Datepicker 导致 Internet Explorer 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8648449/