dポイントプレゼントキャンペーン実施中!

select の option 項目を追加する javascript を組んでいるのですが
IE だけ項目追加ができません。Windows の Firefox2、Safari は期待
通りに項目が追加できました。

option.value を指定しないと IE でも追加できます。IE 専用に特別な
別プログラムが必要なのでしょうか。ご存知の方よろしくお願いします。

var selectBox = document.getElementById("select1");
var option = document.createElement('option');
option.setAttribute('value','my_value');
option.appendChild(document.createTextNode('my_text'));
selectBox.appendChild(option);

3行目をコメントアウトすれば IE でも select にデータが追加され
ますが、肝心の value がないのでプログラム的にダメなんです。

A 回答 (1件)

う~ん・・・Win版IE6では普通に動いてますがどのIEについて言っている


のでしょ?

<script>
window.onload=function(){
var selectBox = document.getElementById("select1");
var option = document.createElement('option');
option.setAttribute('value','my_value');
option.appendChild(document.createTextNode('my_text'));
selectBox.appendChild(option);
}
</script>
<select id="select1" onchange="alert(this.value)">
<option value="">選択する</option>
</select>
    • good
    • 0
この回答へのお礼

回答ありがとうございます。

IEはWin版の6と7です。Macは手元にないのでわかりません。
教えていただいたコードのみでは・・・動きます。なんででしょう。

ただ、これでこの書き方自体は間違いでないことがわかりましたので
視点を変えて調査します。ありがとうございました。

お礼日時:2008/04/27 09:00

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!