亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在js中給select 添加options?

如何在js中給select 添加options?

慕俠2389804 2021-09-08 11:07:41
document.getElementById("Mmonth").options.add(new Option(1, 1),0);document.getElementById("Mmonth").options.add(new Option(1, 1));都已經試過 火狐和google瀏覽器不支持 幫忙 謝謝
查看完整描述

2 回答

?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

我在火狐試了,我這沒問題,代碼
<select id="Mmonth">
<option>1</option>
</select>
<input type="button" onclick="a()" value="添加"/>
<script>
function a(){
document.getElementById("Mmonth").options.add(new Option(1, 1));
}

</script>

查看完整回答
反對 回復 2021-09-13
?
慕村225694

TA貢獻1880條經驗 獲得超4個贊

<script type="text/javascript">
function addList(){
var list = document.getElementById("list");
var bb=["javascript1","javascript2","javascript3","javascript4"];
for(var i = 0; i <bb.length; i++){
var newOption = document.createElement("option");
newOption.setAttribute("value", i);
newOption.appendChild(document.createTextNode(bb[i]));
list.appendChild(newOption);
}
}
function clearList(){
var list = document.getElementById("list");
for(var i = list.options.length; i >= 0; i--){
list.remove(i);
}
}
</script>
</head>
<body>
<select name="list" id="list" style="width:120px">
</select>
<input type="button" onclick="addList()" value="生成"/>
<input type="button" onclick="clearList()" value="清空" />
</body>

 


查看完整回答
反對 回復 2021-09-13
  • 2 回答
  • 0 關注
  • 516 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號