答案 0 :(得分:1)
答案 1 :(得分:1)
出于效率考虑,我想对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>
答案 2 :(得分:0)
如何仅解析输入年份<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">
var startYear = 1800;
for (i = new Date().getFullYear(); i > startYear; i--)
{
$('#yearpicker').append($('<option />').val(i).html(i));
}
</script>