【发布时间】:2019-09-26 17:06:52
【问题描述】:
我有以下代码
<script>
$(document).ready(function() {
$(".datepicker").datepicker( {
year = today.getFullYear,
format: " yyyy", // Notice the Extra space at the beginning
viewMode: "years",
minViewMode: "years",
yearRange: '1920 : year',
autoclose: true,
});
});
</script>
如你所见,我只需要年份,但我不想选择接下来的年份...我的意思是 2020 年、2021 年、2022 年...甚至应该无法选择...我看了周围但找不到解决方案,有人可以帮我吗?谢谢
编辑:
<script>
$(document).ready(function() {
$('#datepicker').keyup(function() {
var inputVal = $(this).val();
var dateReg = /^\d{4}$/;
if (dateReg.test(inputVal)) {
$("#datemsg").html("Valid").css("color", "green");
} else {
$("#datemsg").html("Invalid date").css("color", "red");
}
});
$("#datepicker").datepicker({
format: " yyyy", // Notice the Extra space at the beginning
viewMode: "years",
minViewMode: "years",
autoclose: true,
endDate: new Date(),
});
});
</script>
【问题讨论】:
-
您不能将变量赋值放在对象中。
-
您似乎在 jQuery UI 和 Bootstrap Datepickers 之间混合使用选项。 Bootstrap 没有
yearRange选项,文档是here。 -
也没有
viewMode选项,只有minViewMode和maxViewMode。 -
每个帖子一个问题。这个问题是关于设置最大年份的,正则表达式与它有什么关系?
-
为什么不禁用手动输入而只使用日期选择器?这样他们就不能输入任何无效的内容。
标签: javascript jquery datepicker bootstrap-datepicker