简体   繁体   English

动态添加选项以选择选择标签

[英]Dynamically add options to select select tag

I am trying to write a select drop down which is dynamically filled up with options, the options are the years, eg 2010, 2011, 2012 我正在尝试编写一个选择下拉列表,其中动态填充了选项,这些选项是年份,例如2010、2011、2012

But with each coming year the webpage should add new entry in the drop down with the current year. 但是,随着明年的到来,该网页应该在与当年的下拉列表中添加新条目。 The first entry of the year is 2010 fixed but the last entry is dynamic and depends on the current year.. 该年度的第一项是固定的2010年,但是最后一项是动态的,并且取决于当年。

Any help? 有什么帮助吗?

Something like this? 像这样吗

 var currentYear = new Date().getFullYear(); for (var i = 2010; i <= currentYear; i++) { var selected = ''; if (i == currentYear) selected = 'selected'; $("#years").append("<option value='" + i + "'" + selected + ">" + i + "</option>") } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id='years' /> 

Why don't you select the current year substract 2010 from it ( because it is a fix end) and then you will count up from 2010 adding the amount from your result? 为什么不从中选择当前年份减去2010(因为这是一个固定的结束时间),然后从2010开始加总结果中的金额呢? Thats how I would do that with the first thoughts. 那就是我将如何用最初的想法做到这一点。

Edit: If you fill the dropdown on each load of the page you always fill that array with the amount calculated from your substraction. 编辑:如果您在页面的每次加载中填充下拉列表,则始终使用从您的减法计算出的数量填充该数组。 I don't get the problem. 我没问题。

This is one way of doing it .. fiddle 这是做它的一种方式.. 小提琴

 $("select").append("<option value='added'>added</option>");

append option according to your logic. 根据您的逻辑追加选项。

Here is the jsfiddle link (without jquery). 这是jsfiddle链接(没有jquery)。

<form>
<select id="mySelect">
</select>
</form>

Javascript: Javascript:

var x = document.getElementById("mySelect");
var to = new Date().getFullYear();
var option;
for (var i = 2010; i <= to; i++)
{
    option = document.createElement("option");
    option.text = i;
    x.add(option);
}

Just use the actual date : 只需使用实际日期:

 var startYear = 1950;
 var currentYear = new Date().getFullYear();

And then you can generate your select options 然后您可以生成选择选项

var select = '<select>';
for (var i = startYear; i <= currentYear; i++){
     select += '<option>'+i+'</option>';
}
select += </select>;
var date = new Date();
var year = date.getFullYear(); 

for (var i=2010;i<year+1;i++) {
   $('#MySelect').append('<option value="'+i+'">'+i+'</option>')
}

FiDDLE 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 动态地(以编程方式)添加新选项来进行踢 <select> jQuery中的标签 - Dynamically (programatically) add new options to dropkick <select> tag in jQuery 使用Javascript动态添加选择选项 - Dynamically Add Select Options with Javascript 使用动态选项动态生成选择标签,并在选择一个选项时添加生成另一个选择标签 - Dynamically generating select tags with dynamic options and add generate another select tag when an option is selected 向动态生成的选择标记添加选项 - Adding options to dynamically generated select tag 如何将选项(选项以“ java.sql.ResultSet”的形式)动态添加到jsp中的select标记 - how to dynamically add the options(options are in form of“java.sql.ResultSet”) to the select tag in jsp 如何使用JavaScript向选项标签添加选项? - How to add options to a select tag with JavaScript? 在其他选择“标签”的选项中添加“选定”属性 - Add “selected” attr to options in different select “tag” JQuery 添加选项以动态选择 7 个不同的选择 - JQuery add options to select dynamically for 7 different selects 使用Javascript中的选项动态添加输入类型选择 - Dynamically add input type select with options in Javascript 使用选择选项标签动态添加输入字段 - Dynamically add input field with select option tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM