我想在选择输入类型中将年份限制为仅显示从现在开始的5年和前一年
<div class="col-12">
<select class="form-select bg-light border-0" style="height: 55px;">
<option selected>Years</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
<option value="2021">2021</option>
<option value="2022">2022</option>
</select>
</div>
的例子如上,但使用动态代码与PHP或javascript,请帮助我😥
$(function () {
const currentYear = new Date().getFullYear();
var oldYearValue=0, newYearValue=0;
for (var i = 5; i >= 0; i--) {
debugger
oldYearValue= currentYear-i;
if(oldYearValue != new Date().getFullYear()){
$('#select1').append('<option value='+oldYearValue+'>'+oldYearValue+'</option>');
}
if (i==0){
for (var j = 0; j <= 5; j++) {
newYearValue= currentYear+j;
$('#select1').append('<option value='+newYearValue+'>'+newYearValue+'</option>');
}
}
}
});
<script type="text/javascript" src="js/script.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<div class="col-12">
<select class="form-select bg-light border-0" id="select1" style="height: 55px;">
<option selected>Years</option>
</select>
</div>
见Result Here
可能对你的PHP有帮助
<div class="col-12">
<select class="form-select bg-light border-0">
<option disabled selected>Years</option>
<?php
for ($i = 4; $i >= 0; $i--) {
$year_= date('Y', strtotime("-$i year"));
echo '<option value="'.$year_.'">'.$year_.'</option>';
}?>
</select>