jQuery 日期选择器默认格式无效
jQuery datepicker default format not working
这是我的 .js 文件中的内容:
var ready;
ready = function () {
$.datepicker.setDefaults({
dateFormat: 'yy-mm-dd'
});
};
$(document).ready(ready);
但日期选择器格式仍然是默认格式:mm/dd/yy
背景:
- 我在 Ruby Rails
- 也使用 bootstrap-datepicker-rails gem。有冲突吗?
快速浏览了一下 jQuery Datepicker api documentation,我发现你可以做到这一点
$( ".selector" ).datepicker({ dateFormat: "yy-mm-dd" });
您的代码可能 运行 在日期选择器已经初始化之后。您应该能够在 JavaScript.
中初始化日期选择器的任何地方传递 dateFormat: "yy-mm-dd"
选项
您应该能够在 dateFormat 初始化后更新它(基于 these docs),如下所示:
// Setter
$(".selector").datepicker("option", "dateFormat", "yy-mm-dd");
.selector
在您的情况下很可能是 .datepicker
,除非您更改了它(examples here)。
此外,您应该能够看到日期格式设置如下:
console.log("dateFormat:", $(".selector").datepicker("option", "dateFormat"));
这样试试:
$.extend($.fn.datepicker.defaults, {
format:'yyyy-mm-dd',
autoclose: true,
todayHighlight: true,
showOnFocus: false
});
这是我的 .js 文件中的内容:
var ready;
ready = function () {
$.datepicker.setDefaults({
dateFormat: 'yy-mm-dd'
});
};
$(document).ready(ready);
但日期选择器格式仍然是默认格式:mm/dd/yy
背景: - 我在 Ruby Rails - 也使用 bootstrap-datepicker-rails gem。有冲突吗?
快速浏览了一下 jQuery Datepicker api documentation,我发现你可以做到这一点
$( ".selector" ).datepicker({ dateFormat: "yy-mm-dd" });
您的代码可能 运行 在日期选择器已经初始化之后。您应该能够在 JavaScript.
中初始化日期选择器的任何地方传递dateFormat: "yy-mm-dd"
选项
您应该能够在 dateFormat 初始化后更新它(基于 these docs),如下所示:
// Setter
$(".selector").datepicker("option", "dateFormat", "yy-mm-dd");
.selector
在您的情况下很可能是 .datepicker
,除非您更改了它(examples here)。
此外,您应该能够看到日期格式设置如下:
console.log("dateFormat:", $(".selector").datepicker("option", "dateFormat"));
这样试试:
$.extend($.fn.datepicker.defaults, {
format:'yyyy-mm-dd',
autoclose: true,
todayHighlight: true,
showOnFocus: false
});