下拉列表中的HTML循环选项值 [英] HTML looping option values in drop down list

查看:205
本文介绍了下拉列表中的HTML循环选项值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图做一个表格,其中包含一个关于当前年龄的问题,我已经决定回答这个问题的最简单方法是填写一个下拉列表。所以我在下拉列表中的第一个值应该是1900,然后它将增加一直到2014年。我该怎么做?

I have been trying to do a form where a question about one's current age is included, and I have decided the easiest way to answer this question is by filling in a drop down list. So my first value in the drop down list shall be 1900 and then it shall increment by one till it reaches 2014. How do I do that?

推荐答案

我不会设定一个固定的最后一年,为什么明年会再次重新编码?

I wouldn't set a fixed final year, why recode again next year?

请注意,更新DOM一次比更新DOM更有效每年的DOM添加到列表中。

Note that it is more effecient to update the DOM once than updaing the DOM for each year added to the list.

HTML

<select id="year"></select>

脚本

var start = 1900;
var end = new Date().getFullYear();
var options = "";
for(var year = start ; year <=end; year++){
  options += "<option>"+ year +"</option>";
}
document.getElementById("year").innerHTML = options;

示例

这篇关于下拉列表中的HTML循环选项值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆