在 jQuery 中生成下拉菜单的年份

发布于 2024-11-02 19:30:56 字数 139 浏览 0 评论 0原文

我想到了一个小问题,我正在尝试想出一种方法来解决它。

基本上,我想要做的是将年份减去 18 年,即:2011 年返回 1993 年,并生成一个下拉选项,供用户选择在此之前的任何年份。

基本上他们必须证明自己已超过法定年龄 18 岁。

I came up with a small issue, I am trying to come up with a way to fix it.

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.

Basically they have to show that they are over the legal age of 18.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青芜 2024-11-09 19:30:56
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/

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>

Demo: http://jsfiddle.net/DhpBg/.

晌融 2024-11-09 19:30:56
var minOffset = 0, maxOffset = 100; // Change to whatever you want // minOffset = 0 for current year 
var thisYear = (new Date()).getFullYear();
var m_names = ['January', 'February', 'March','April', 'May', 'June', 'July','August', 'September', 'October','November', 'December'];
var month = 0   // month = (new Date()).getMonth(); // for cuurent month
for (var j = month; j <= 11; j++) {var months = m_names[ 0 + j].slice( 0, 3 ); $('<option>', {value: months, text: months}).appendTo(".month"); }
for (var i = minOffset; i <= maxOffset; i++) { var year = thisYear + i; $('<option>', {value: year, text: year}).appendTo(".year");}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select class="year">

</select>

<select class="month">

</select>

jquery 更好的月份和年份解决方案

www.jsfiddle.net/sudharshanreddyjanike/byvotshz/2/

var minOffset = 0, maxOffset = 100; // Change to whatever you want // minOffset = 0 for current year 
var thisYear = (new Date()).getFullYear();
var m_names = ['January', 'February', 'March','April', 'May', 'June', 'July','August', 'September', 'October','November', 'December'];
var month = 0   // month = (new Date()).getMonth(); // for cuurent month
for (var j = month; j <= 11; j++) {var months = m_names[ 0 + j].slice( 0, 3 ); $('<option>', {value: months, text: months}).appendTo(".month"); }
for (var i = minOffset; i <= maxOffset; i++) { var year = thisYear + i; $('<option>', {value: year, text: year}).appendTo(".year");}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select class="year">

</select>

<select class="month">

</select>

better solution for month and year by jquery

www.jsfiddle.net/sudharshanreddyjanike/byvotshz/2/

可是我不能没有你 2024-11-09 19:30:56

您可以在本机 Javascript 中轻松完成此操作,无需使用任何 jQuery。

演示: http://jsbin.com/orasa5/2/

编辑:不确定我为什么要创建每个选项都有一个节点。我猜现在是凌晨。

You can do this in native Javascript without any jQuery pretty easily.

Demo: http://jsbin.com/orasa5/2/

Edit: Not sure why I was creating a node for every option. It's early in the morning, I guess.

迟到的我 2024-11-09 19:30:56
<select name="Year" class="bdayyear">
    <option value="year">Year</option>
    <script language="javascript">
                for (var i = 1960; i < 2010; i ++) {
                    document.write("<option value=\"" + i + "\">" + i + "</option>\n");
                }
            </script>
    </select>

我希望这会有所帮助,因为这就是如何做到这一点的。

<select name="Year" class="bdayyear">
    <option value="year">Year</option>
    <script language="javascript">
                for (var i = 1960; i < 2010; i ++) {
                    document.write("<option value=\"" + i + "\">" + i + "</option>\n");
                }
            </script>
    </select>

I hope that helps because that is pretty much how to do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文