我有3个选择标签显示日期/月/年,我目前正在显示当年的年份列表,即2017年我想要与蛾一起工作。以下是我今年使用的javascript代码
<select name="month" >
<option value="">select</option>
</select>
<select name="day" >
<option value="">select</option>
</select>
<select name="year">
<option value="">select</option>
</select>
<script type="text/javascript">
var d = new Date();
var n = d.getFullYear();
for (var i = n; i >= 1950; i--) {
var opt = new Option();
opt.value = opt.text = i;
kcyear.add(opt);
}
</script>
答案 0 :(得分:0)
我循环遍历每个选项元素并检查value属性是否大于当前月份值。如果是我隐藏那个元素。
<select name="month">
<option selected value="">select</option>
<option value='1'>January</option>
<option value='2'>February</option>
<option value='3'>March</option>
<option value='4'>April</option>
<option value='5'>May</option>
<option value='6'>June</option>
<option value='7'>July</option>
<option value='8'>August</option>
<option value='9'>September</option>
<option value='10'>October</option>
<option value='11'>November</option>
<option value='12'>December</option>
</select>
<script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script>
<script type="text/javascript">
var currentMonth = new Date().getMonth() + 1;
$("option").each(function() {
if ($(this).val() > currentMonth) {
$(this).css("display", "none");
}
});
</script>