[英]Failing to populate the Options in Select element using javascript
The following code is able to generate the Select drop down list but it is not being populated with options. 以下代码能够生成“选择”下拉列表,但是未填充选项。 I am trying to create a 'day of the month' calendar. 我正在尝试创建“每月的某天”日历。 I am not sure where my code is wrong... 我不确定我的代码在哪里...
var select_day = document.createElement('select');
select_day.setAttribute('id', 'select_day');
var dayArray = []; // populate day array
for(var i=0; i <= 30; i++){
dayArray[i] = i + 1;
}
console.log(dayArray);
// DATE - create and append options
for(var i=0; i< dayArray.length; i++){
var option = document.createElement('option');
option.setAttribute('value', dayArray[i]);
option.setAttribute('text', dayArray[i]);
select_day.appendChild(option);
}
The attribute that controls (in the absence of a child text node) the display text of an option is called label
, not text
. 控制(在没有子文本节点的情况下)选项显示文本的属性称为label
,而不是text
。
option.setAttribute('text', dayArray[i]);
should be 应该
option.setAttribute('label', dayArray[i]);
var select_day = document.createElement('select'); select_day.setAttribute('id', 'select_day'); var dayArray = []; // populate day array for (var i = 0; i <= 30; i++) { dayArray[i] = i + 1; } console.log(dayArray); // DATE - create and append options for (var i = 0; i < dayArray.length; i++) { var option = document.createElement('option'); option.setAttribute('value', dayArray[i]); option.setAttribute('label', dayArray[i]); select_day.appendChild(option); } document.body.appendChild(select_day);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.