我有这样的 jquery datepicker
$birth.datepicker({
onSelect: function(dateText, inst) {
$(this).parent().find("label#error").html("");
$gender.focus();
},
yearRange: '1950:2006',
changeMonth: true,
changeYear: true,
showOtherMonths: true,
selectOtherMonths: true,
dateFormat: 'yy-mm-dd',
defaultDate: new Date(2006,01,01),
});
我正在尝试修改这段代码,这样它就可以像这样将当前年份减去 10
$birth.datepicker({
var currentYear = new Date();
onSelect: function(dateText, inst) {
$(this).parent().find("label#error").html("");
$gender.focus();
},
yearRange: '1950:document.write(currentYear.getFullYear() - 10)',
changeMonth: true,
changeYear: true,
showOtherMonths: true,
selectOtherMonths: true,
dateFormat: 'yy-mm-dd',
defaultDate: new Date(2006,01,01),
});
上面的代码不起作用,它破坏了我的日期选择器....有更好的方法吗?
最佳答案
改变:
yearRange: '1950:document.write(currentYear.getFullYear() - 10)',
到:
yearRange: '1950:-10',
这会将 yearRange 从 1950 设置为比当前年份小 10 年。
来自documentation对于 yearRange 选项:
The range of years displayed in the year drop-down: either relative to today's year ("-nn:+nn"), relative to the currently selected year ("c-nn:c+nn"), absolute ("nnnn:nnnn"), or combinations of these formats ("nnnn:-nn").
关于javascript - 当前年份的 jQuery 日期选择器 - 10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14840800/