javascript - jquery ui datepicker setdate 仅适用于 google chrome

标签 javascript jquery jquery-ui datepicker jquery-ui-datepicker

我的代码:

<input type="text" id="testdate" name="testdate" value="2017-01-01" />

<script>
$('#testdate').datepicker();
$('#testdate').datepicker('option', 'dateFormat', 'yy-mm-dd');
$("#testdate").datepicker('setDate', new Date('2017-01-01 12:00:00'));
</script>

JS fiddle :https://jsfiddle.net/g0uyok13/

在 Google Chrome 中,它会在文本输入区域中显示 2017-01-01。在 Firefox 中,它显示今天的日期 (atm 2016-02-24)。我希望它在 Firefox 中的工作方式与在 Google Chrome 中的工作方式相同。

有什么想法吗?

最佳答案

问题出在时间 12:00:00 。不要忘记您正在使用它是一个日期选择器。时间与此无关。

现在它在 Chrome 中工作的原因,我猜这是浏览器的内部实现。 Mozilla 会丢弃无效的日期格式,而 chrome 会尝试将其删除......(这纯粹是我的假设,我还没有基于任何文档......)

更新

是的,我的假设是正确的。 Firefox不支持您输入的日期格式,而 chrome 更灵活并且支持它。对于通用日期和时间,您可以使用以下代码:

$('#testdate').datepicker();
$('#testdate').datepicker('option', 'dateFormat', 'yy-mm-dd');
$("#testdate").datepicker('setDate', new Date('2017-01-01T12:00:00Z'));

(在两种浏览器中均适用)

您还可以查看new Date() works differently in Chrome and Firefox了解更多信息

关于javascript - jquery ui datepicker setdate 仅适用于 google chrome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35614633/

相关文章:

javascript - 如何在 Javascript 中从外部作用域调用函数?

C# Web 服务不会输出 JSON,只输出 XML

在 chrome 中完成检查元素后,Jquery 工作

jquery-ui - jQueryui 选项卡中的多个 jQgrid

javascript - NodeJS 模块导出/原型(prototype) - 没有方法

javascript - 在 div 中预览多个图像时出错

javascript - 使用 Asp.net web api 下载时出错

jquery - 为什么我的div不淡出

javascript - JQuery Sortable : Making sortable elements non-draggable, 还可以排序

javascript - 使用 JQuery 动态调整 2 列 HTML 页面的大小会导致 IE6.5 上的无限循环