在jQuery中生成下拉列表的年数 [英] Generate years for drop down in jQuery
本文介绍了在jQuery中生成下拉列表的年数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我提出了一个小问题,我正在尝试提出一种解决方法.
I came up with a small issue, I am trying to come up with a way to fix it.
我基本上想做的是每年放18年假,即:返回1993年的2011年,并生成一个下拉选项供用户选择在此之前的任何年份.
Basically what I want to do is take 18 years off the year ie: 2011 which returns 1993, and generate a drop down option for a user to select any year before that.
基本上,他们必须证明自己已经超过18岁的法定年龄.
Basically they have to show that they are over the legal age of 18.
推荐答案
var minOffset = 18,
maxOffset = 100;
var thisYear = new Date().getFullYear();
var select = $('<select>');
for (var i = minOffset; i <= maxOffset; i++) {
var year = thisYear - i;
$('<option>', { value: year, text: year }).appendTo(select);
}
select.appendTo('body');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
演示: http://jsfiddle.net/DhpBg/.
这篇关于在jQuery中生成下拉列表的年数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文