在Datepicker jQuery中设置最大年份

时间:2019-09-26 17:06:52

标签: javascript jquery datepicker bootstrap-datepicker

我有以下代码

<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>

1 个答案:

答案 0 :(得分:1)

您需要将year连接到您的范围字符串,因为js无法解析此变量:

    year = today.getFullYear,
    $(".datepicker").datepicker({
        format: " yyyy", // Notice the Extra space at the beginning
        viewMode: "years", 
        minViewMode: "years",
        yearRange: '1920 : ' + year,
        autoclose: true,            
     });