HTML5 输入类型只显示年份

HTML5 input type show only years

也许这个问题已经问过了,但这并不能解决我的问题。

这是我在输入框中的查询,我只想显示这样的年份

提前致谢

Note: Already this qustions asked but it not solve my issue

请尝试select and show year only. And guide is here

如何解析只输入年份<input date="year">

您可以使用此代码段。

PHP版本:

  <select class="form-control" name="startyear">
   <?php
   for ($year = (int)date('Y'); 1900 <= $year; $year--): ?>
     <option value="<?=$year;?>"><?=$year;?></option>
   <?php endfor; ?>
  </select>

JavaScript 版本:

    <select name="yearpicker" id="yearpicker"></select>

    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script type="text/javascript">
    let startYear = 1800;
    let endYear = new Date().getFullYear();
    for (i = endYear; i > startYear; i--)
    {
      $('#yearpicker').append($('<option />').val(i).html(i));
    }
    </script>

为了效率起见,我想对 Rhalp 的回答做一个小的更正。不要初始化对象 219 次。一般来说,如果你在循环之前获得成员,那么在浏览器引擎上的工作就会少得多。

    <select name="yearpicker" id="yearpicker"></select>

    <script type="text/javascript"
            src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"> 
    </script>
    <script type="text/javascript">
    let startYear = 1800;
    let endYear = new Date().getFullYear();
    for (i = endYear; i > startYear; i--)
    {
      $('#yearpicker').append($('<option />').val(i).html(i));
    }
    </script>