我试图在 select - option 标签中填充数字。但是我的脚本只在一个选择标签中填充数字
HTML:
<div class="form_row">
<label>Week Num</label>
<select class="form_select1" id="weeknum"></select>
<b class="bold1">  TO  </b>
<select class="form_select1" id="weeknum1"></select>
</div>
Javascript:
<script>
var select = document.getElementById("weeknum");
var select1 = document.getElementById("weeknum1");
for (var i = 52; i >= 1; i--) {
var option = document.createElement('option');
option.text = option.value = i;
select.add(option, 0);
select1.add(option, 0);
}
</script>
我该如何实现?
最佳答案
您只能将一个元素添加到父节点一次,因此您需要创建另一个选项元素:
var select = document.getElementById("weeknum");
var select1 = document.getElementById("weeknum1");
for (var i = 52; i >= 1; i--) {
var option = document.createElement('option');
option.text = option.value = i;
var option1 = document.createElement('option');
option1.text = option1.value = i;
select.add(option, 0);
select1.add(option1, 0);
}
这是 JSFIddle .
关于javascript - 自动填充选项标签不工作 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27638828/