我的代码:
<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/