select option value が IE だけで動かない

このQ&Aのポイント
  • 質問者は、select要素にoption要素を追加するJavascriptを組んでいるが、IEでは項目追加ができないという問題に遭遇している。
  • WindowsのFirefox2とSafariでは期待通りに項目が追加できたが、IEではoption要素のvalueを指定しない場合には項目追加ができる。
  • 質問者は、IE専用に特別なプログラムが必要なのかを知りたいとしている。
回答を見る
  • ベストアンサー

select option value が IE だけで動かない

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 がないのでプログラム的にダメなんです。

  • s12s
  • お礼率100% (1/1)

質問者が選んだベストアンサー

  • ベストアンサー
  • yambejp
  • ベストアンサー率51% (3827/7415)
回答No.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>

s12s
質問者

お礼

回答ありがとうございます。 IEはWin版の6と7です。Macは手元にないのでわかりません。 教えていただいたコードのみでは・・・動きます。なんででしょう。 ただ、これでこの書き方自体は間違いでないことがわかりましたので 視点を変えて調査します。ありがとうございました。

関連するQ&A

  • テーブルにおける行(セルにプルダウン含む)の追加方法

    追加ボタンを押度に,テーブルに1行ずつ追加されるプログラムを組んでいるのですが,追加ボタンを押しても何も起こりません.1行には,テキストボックスとプルダウンメニューの2つのセルが含まれます.テキストボックスのみであれば,追加に成功します. プルダウンのメニュー項目の追加方法ではなく,プルダウン自体を複製して追加していきたいのですが,参考サイトが見つかりませんでした.また,各行を識別する為一意の名前をつけたいのですが,どう記述したらよろしいでしょうか. お手数おかけしますが,下記ソースのどこが間違っているのか,どう処理すべきなのかを,ご教示頂けますでしょうか.お願い致します. <BODY> <TABLE id="Table1"> <TR> <td class="cellWhite"><input type="text" value="単語入力" onFocus="cText(this)" onBlur="sText(this)" style="color:#999"></td> <td bgcolor="#5096C9" width="113px"> <select name="column"> <option value="00">赤</option> <option value="01">青</option> <option value="02">黄</option> </select></td></tr></TABLE> <INPUT TYPE="button" VALUE="行追加" onclick="addRow()"> <script language="JavaScript"> function addRow() {  var tbl = document.getElementById("Table1").firstChild;  if (!tbl.tagName) {    tbl = document.getElementById("Table1");}  var tr = document.createElement("tr");  var td1 = document.createElement("td");  td1.setAttribute("class","cellWhite");  var tx1 = document.createElement("input");  tx1.setAttribute("type","text");  tx1.setAttribute("value","単語入力");  tx1.setAttribute("onFocus","cText(this)");  tx1.setAttribute("onBlur","sText(this)");  tx1.setAttribute("style","color:#999");  td1.appendChild(tx1);  var td2 = document.createElement("td");  var selectName="column";//selectの名前  var options={0:"赤",1:"青",2:"黄"};  var c=sel.firstChild;  while(c){   if(c.name==selectName) sel.removeChild(c);     c=c.nextSibling;}   var e=document.createElement("select");   e.setAttribute("name",selectName);   for(var i in options){     var o=document.createElement("option");     o.setAttribute("value",options[i]);     o.appendChild(document.createTextNode(options[i]));     e.appendChild(o);}   sel.appendChild(e);   tr.appendChild(td1);tr.appendChild(td2);tbl.appendChild(tr);}</script> 参考にしたサイトです: http://oshiete1.watch.impress.co.jp/qa4074195.html http://okwave.jp/qa299889.html

  • いつも参考にさせて頂いています。

    いつも参考にさせて頂いています。 javascriptでセレクトメニューを作っているのですが うまく出来ないのでどなたか教えて下さい。 宜しくおねがいします。 3連動するセレクトメニューで 3つ目のセレクトメニューを選ぶと 選ばれた項目に対応するURLに飛ぶように作っています。 ここからが質問なのですが、 ページに飛んだ時にセレクトメニューで選択した内容が消えてしまいます・・・ 飛んだ先のページでも、選択した内容を表示させるには どのようにすればいいのでしょうか? ご存知の方がいましたら教えて下さい。 ---html部分---- <html> <head> <script type="text/javascript" src="test.js"></script> </head> <body> <form id="f0"> <select id="s0"> <option value="">----------------</option> </select> <select id="s1"> <option value="">----------------</option> </select> <select id="s2"> <option value="">----------------</option> </select> </form> </body> </html> ---外部javascriptファイル---- var data= { 吉野家: { "牛丼(並)":{0:"380円"}, "牛丼(大盛)":{1:"480円"}, "牛丼(特盛)":{2:"630円"} } ,松屋: { "牛丼(並)":{3:"320円"}, "牛丼(大盛り)":{4:"420円"}, "牛丼(特盛)":{5:"520円"} } ,すき家: { "牛丼(並)":{6:"280円"}, "牛丼(大盛)":{7:"380円"}, "牛丼(特盛)":{8:"480円"} }; window.onload=function(){ s0=document.getElementById("s0"); s1=document.getElementById("s1"); s2=document.getElementById("s2"); setSelect(data,s0); s0.onchange=function(){ clearSelect(s2); if(this.value==""){ clearSelect(s1); }else{ setSelect(data[this.value],s1); } } s1.onchange=function(){ if(this.value==""){ clearSelect(s2); }else{ setSelect(data[s0.value][this.value],s2); } } s2.onchange=function(){ location.href="/////////"; } } function setSelect(obj,sel){ while(lc=sel.lastChild){ sel.removeChild(lc); }; var op=document.createElement("option"); op.setAttribute("value",""); op.appendChild(document.createTextNode("----------------")); sel.appendChild(op); for(var i in obj){ var txt=(typeof obj[i]=="object")?i:obj[i]; var op=document.createElement("option"); op.setAttribute("value",i); op.appendChild(document.createTextNode(txt)); sel.appendChild(op); } } function clearSelect(sel){ while(lc=sel.lastChild){ sel.removeChild(lc); }; var op=document.createElement("option"); op.appendChild(document.createTextNode("----------------")); sel.appendChild(op); };

  • 「option」の追加におけるselectedの指定での動作は?

    「createElement」を用いて、ドロップダウンのoptionを追加するスクリプトを 作成しました。 このドロップダウンでは、月・日を選択するもので当日の日付をselected の属性を 付けたいと考え、つぎのコードを作成しましたが、当月が選択されたところで スクリプトが止まってしまう(抜けている)のです。 function appendTest(frmParts1,frmParts2,frmParts3){ if (!document.createElement) return; strDate = new Date(); for (ii = 1;ii < 13;ii++){ var ele = document.createElement("option"); ele.value = ii; var str = document.createTextNode(ii); ele.appendChild(str); document.info_inp.elements[frmParts2].appendChild(ele); } // alert("ok1"); document.info_inp.elements[frmParts2].options[strDate.getMonth()].selected = true; for (ii = 1;ii < 32;ii++){ var ele = document.createElement("option"); ele.value = ii; var str = document.createTextNode(ii); ele.appendChild(str); document.info_inp.elements[frmParts3].appendChild(ele); } // alert("ok2"); document.info_inp.elements[frmParts3].options[strDate.getDate()-1].selected = true; } コメントにしている alert(); を生かすと、想定どおりに当日の日付が選択されますが、 何故alert(); を外すと想定した動作が行われないのかがわかりません。 selectedを指定する行を外せば全く問題ない動作をするようですが、これでは 当日の日付が選択された状態にはなりません。 なんとかして「日付が選択された状態」にしたいのですが、何らかの処理が 欠けているのでしょうか。 どなた様かご教示願います。

  • スマートフォンで動かない

    http://yoneyone.my-sv.net/XmlSelectBox.htm ページですが以下のjavascriptを修正しスマートフォンで動かす事は可能でしょうか? 解決できない場合、スマートフォンで動かないjavascriptを記述していると思うのですが、 原因ってなんなのでしょうか? 以下がjavascriptです。 function xmlselectbox(id,xml_url,key,func){ var callback_func=func; var outarea=document.querySelector(id); var xml={}; var items=[]; var XHR= new XMLHttpRequest(); while(outarea.hasChildNodes()){ outarea.removeChild(outarea.lastChild); } //XMLのロード(エラー処理省略) XHR.open("GET",xml_url,false); XHR.send(null); if(XHR.status == 200) xml=XHR.responseXML; //if(xml.hasOwnProperty("SelectionLanguage")) //IEは使えない if(typeof xml.hasOwnProperty === "undefined") //とりあえず xml.setProperty("SelectionLanguage","XPath"); //IEにのみ必用 //最初のSELECTBOX生成 items=getitem("/descendant::node()/child::"+key,xml); var first_contener=document.createElement("div"); var first_selectbox=document.createElement("select"); first_selectbox.setAttribute("name",key); first_selectbox.add(new Option(key+"を選択してください","default"),null); for(var i=0;i<items.length;i++){ first_selectbox.add(new Option(items[i].firstChild.nodeValue,items[i].firstChild.nodeValue),null); } //イベント追加 first_selectbox.onchange=select_change(first_selectbox,xml,outarea); first_contener.appendChild(document.createTextNode(key+":")); first_contener.appendChild(first_selectbox); outarea.appendChild(first_contener); first_selectbox.selectedIndex=0; //SELECTBOXをどんどん作成 function select_change(selectbox,xml,outarea){ return function(){ var items=[]; items=getitem("/descendant-or-self::node()/child::" + selectbox.name + "[contains(self::node()/text(),'" + selectbox.value + "')]/child::*",xml); /* *********************************************************************************** normalize-space()は空白をtrimするだけ。 OK items=getitem("/descendant-or-self::node()/child::" + selectbox.name + "[normalize-space(self::node()/text())='" + selectbox.value + "']/child::*",xml); OK items=getitem("//"+selectbox.name+"[normalize-space(./text())='"+selectbox.value+"']/*",xml); ×? items=getitem("/descendant-or-self::" + selectbox.name +"[normalize-space(.)= '" + selectbox.value + "']/*",xml); */ if(items.length>0){ if(selectbox.parentNode.nextSibling){ if(selectbox.parentNode.nextSibling.lastChild.getAttribute("name")==items[0].nodeName) outarea.removeChild(selectbox.parentNode.nextSibling); } var next_contener=document.createElement("div"); var next_selectbox=document.createElement("select"); next_selectbox.setAttribute("name",items[0].nodeName); next_selectbox.add(new Option(items[0].nodeName+"を選択してください","default"),null); for(var i=0;i<items.length;i++){ next_selectbox.add(new Option(items[i].firstChild.nodeValue,items[i].firstChild.nodeValue),null); } //イベント追加 next_selectbox.onchange=select_change(next_selectbox,xml,outarea); next_contener.appendChild(document.createTextNode(items[0].nodeName+":")); next_contener.appendChild(next_selectbox); if(selectbox.parentNode.nextSibling){ outarea.insertBefore(next_contener,selectbox.parentNode.nextSibling); }else{ outarea.appendChild(next_contener); } next_selectbox.selectedIndex=0; }else{ if(typeof callback_func != "undefined") callback_func(selectbox.getAttribute("name"),selectbox.value); } } } //XPATHで検索 function getitem(xpath,xml){ var nodeValues=[]; var NodeList,Nodelength; try{ NodeList = xml.evaluate(xpath,xml,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null); }catch(e){ NodeList = xml.selectNodes(xpath); } Nodelength=NodeList.snapshotLength||NodeList.length; for(var i=0;i<Nodelength;i++){ nodeValues[i]=(typeof(NodeList.snapshotItem)==='undefined')?NodeList.item(i):NodeList.snapshotItem(i); //console.log(nodeValues[i]); } return nodeValues;

  • テーブルの行削除について

    以前、テーブルの行追加について教えてもらい出来ましたが 逆に、追加された行を削除する方法を教えてください。 <HTML> <HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=Shift_JIS"> <TITLE>行追加・削除</TITLE> </HEAD> <BODY> <TABLE BORDER="1" id="Table1"> <TR> <TH>タイトルA</TH> <TH>タイトルB</TH> </TR> <TR> <TD><INPUT TYPE="text" SIZE="18" MAXLENGTH="18" NAME="A" VALUE=""></TD> <TD><INPUT TYPE="text" SIZE="18" MAXLENGTH="18" NAME="B" VALUE=""></TD> </TR> </TABLE> <INPUT TYPE="button" VALUE="行追加" onclick="addRow()"> <INPUT TYPE="button" VALUE="行削除"> <script language="JavaScript"> function addRow() { var tbl = document.getElementById("Table1").firstChild; if (!tbl.tagName) { tbl = document.getElementById("Table1"); } var tr = document.createElement("tr"); var td1 = document.createElement("td"); var tx1 = document.createElement("input"); tx1.setAttribute("type","text"); tx1.setAttribute("size","18"); tx1.setAttribute("maxlength","18"); tx1.setAttribute("name","Cn"); tx1.setAttribute("value",""); td1.appendChild(tx1); var td2 = document.createElement("td"); var tx2 = document.createElement("input"); tx2.setAttribute("type","text"); tx2.setAttribute("size","18"); tx2.setAttribute("maxlength","18"); tx2.setAttribute("name","Cn"); tx2.setAttribute("value",""); td2.appendChild(tx2); tr.appendChild(td1); tr.appendChild(td2); tbl.appendChild(tr); } </script> </BODY> </HTML>

  • カウントアップ

    xmlの勉強中にわからないことがでてきたので質問します xmlを使用しての掲示板の作成中なのですが 以下のようなソースを記述しました。 //カウントアップ処理 Count count = new Count(); //ノードを作成し、文字列を代入 contribute.setAttribute("no", "1"); Element titlenode= document.createElement("title"); contribute.appendChild(titlenode); titlenode.appendChild(document.createTextNode(title)); Element namenode= document.createElement("name"); contribute.appendChild(namenode); namenode.appendChild(document.createTextNode(name)); Element mailnode= document.createElement("mail"); contribute.appendChild(mailnode); mailnode.appendChild(document.createTextNode(mail)); Element hpnode= document.createElement("hp"); contribute.appendChild(hpnode); hpnode.appendChild(document.createTextNode(hp)); Element urlnode= document.createElement("url"); contribute.appendChild(urlnode); urlnode.appendChild(document.createTextNode(url)); Element textnode= document.createElement("text"); contribute.appendChild(textnode); textnode.appendChild(document.createTextNode(text)); contribute.setAttribute("no", "1"); この行の"1"の部分を1~10などのようにカウントアップさせたいのですが ネットなどで調べてもわかりません。 条件として、Countという別のクラスを作成しそちらでカウントアップしたものを上記ソースで読み込むという処理がしたいのです。 初歩的な質問で情けないのですが、よろしくお願いします。

    • 締切済み
    • XML
  • セレクトボックスについて

    <script type="text/javascript"> function entryChange(){ if(document.getElementById('changeSelect')){ id = document.getElementById('changeSelect').value; if(id == '0'){ document.getElementById('selectbox1').style.display = "none"; document.getElementById('selectbox2').style.display = "none"; document.getElementById('selectbox3').style.display = "none"; } if(id == '1'){ document.getElementById('selectbox1').style.display = ""; document.getElementById('selectbox2').style.display = "none"; document.getElementById('selectbox3').style.display = "none"; } if(id == '2'){ document.getElementById('selectbox1').style.display = "none"; document.getElementById('selectbox2').style.display = ""; document.getElementById('selectbox3').style.display = "none"; } if(id == '3'){ document.getElementById('selectbox1').style.display = "none"; document.getElementById('selectbox2').style.display = "none"; document.getElementById('selectbox3').style.display = ""; } } } window.onload = entryChange; </script> ↑こちらは表示/非表示の処理をしています。 <select id="changeSelect" name="number" onchange="entryChange();"> <option value="0">未選択</option> <option value="1">リスト1</option> <option value="2">リスト2</option> <option value="3">リスト3</option> </select> <select name="number" id="selectbox1" style="display:none"> <option value="東京都">東京都</option> <option value="京都府">京都府</option> </select> <select name="number" id="selectbox2" style="display:none"> <option value="北海道">北海道</option> <option value="沖縄県">沖縄県</option> </select> <select name="number" id="selectbox3" STYLE="display:none"> <option value="兵庫県">兵庫県</option> <option value="大阪府">大阪府</option> </select> <input type="submit" value="送信"> (1)「未選択」のまま送信すると「大阪府」が送信されます。 (2)「リスト1」→「沖縄県」を選択 「大阪府」が送信されます。(リスト2も同じ事になります。) (3)「リスト3」→「兵庫県」を選択 「兵庫県」と送信されます。 リスト3以外を選択するとなぜか「大阪府」が送信されます。 初心者なりに色々調べたのですが解決できず困っております。 よろしくお願いします。

  • selectのonChangeが動作しません

    createElementで、フォームを生成しています。 コンボボックスが変更されたら、アラームを表示させるようにしたいのですが上手くいきません。 どのようにしたら、良いのでしょうか? よろしくお願いします。 以下ソースになります。 上から24行目付近で、selectにonChangeを設定しています。 -------------------------------------------------------------------- <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>テスト</title> <script type="text/javascript"> function test() { var buf = document.createElement('div'); buf.innerHTML = document.form1.src.value; var nodes = buf.querySelectorAll('*[class="line"]'); //formの生成 var Frm = document.getElementById("setform"); //要素を表示する場所(親要素) var Form = document.createElement("form"); //新規にform要素を生成 Form.name = "dynform"; Frm.appendChild(Form); //Frmの子要素としてFormを追加 // select要素 コンボボックス作成 var Slt = document.createElement("select"); //selectタグを生成 Slt.name = "stasel"; Slt.id = "stasel"; Slt.onChange = "alert('今だ')"; //この部分でselectにonChange を設定している Slt.size="1"; //コンボボックス用にサイズを1にする for (var i=0; i<nodes.length; i++) { var Ops = document.createElement("option"); if (/Firefox/.test(navigator.userAgent)) { var regm = nodes[i].rows[0].cells[0].textContent.match(/20[0-9][0-9]年[0-1]?[0-9]月/); } else { var regm = nodes[i].rows[0].cells[0].innerText.match(/20[0-9][0-9]年[0-1]?[0-9]月/); } var listval = document.createTextNode(regm); //生成する要素の文字列 Ops.value = regm; Ops.appendChild(listval); //Spnに表示する文字列追加 Slt.appendChild(Ops); //Sltの子要素としてOpsを追加 } Frm.appendChild(Slt); //Frmの子要素としてSltを追加 var Br=document.createElement("br"); //改行 Frm.appendChild(Br); //Frmの子要素としてBrを追加 var Br=document.createElement("br"); //改行 Frm.appendChild(Br); //Frmの子要素としてBrを追加 document.getElementById('stasel').options[0].selected = true; } // --> </script> </head> <body> <p>自動出力</p> <form name="form1"> <p>テキストエリアにソースを張り付けて下さい</p> <p> <textarea id="src" rows="5" cols="200"> </textarea> </p> <p> <input type="button" value="実行" onClick="test()"> </p> </form> <div id="setform"> </div> </body> </html> -------------------------------------------------------------------- 以下の文をテキストエリアに入力しています。 -------------------------------------------------------------------- <table class="line"> <tr> <td>2011年1月14日</td> </tr> </table> <table class="line"> <tr> <td>2011年2月14日</td> </tr> </table> <table class="line"> <tr> <td>2011年3月14日</td> </tr> </table> <table class="line"> <tr> <td>2011年4月14日</td> </tr> </table> --------------------------------------------------------------------

  • borderの指定方法

    var tbl = document.createElement("table"); var tbd = document.createElement("tbody"); var tr = document.createElement("tr"); var td = document.createElement("td"); td.appendChild(document.createTextNode("テストテーブル")); tr.appendChild(td); tbd.appendChild(tr); tbl.appendChild(tbd); DOMで書いたテーブルのTD要素のボーダーの指定方法で、下線だけ表示しないで枠をつけたいのですが、どのようにすればよいのでしょうか? どなたか教えてください。 よろしくお願いします。

  • 記述方法について

    よろしくお願いします。前回こちらで教えて頂いたソースを 基に表示するテーブルの並び方を変えたいと思っています。 理想の並び方は、例として くま さる           くま  さる ごりら  これらを→ごりら うま  うま           りす りす というように読み込んだエクセルのレコードを2列ずつにして表したいのですが、可能でしょうか? 以下のソースを基に、色々試したのですが、ブラウザ上には何も出てきません。 -------------------------------------------------------------- <html> <head> <title>TEST CSV IN</title> <script type="text/javascript"> function hoge(){ var TEST = document.getElementById("TEST"); var output = document.getElementById("OUTPUT"); var table = document.createElement("table"); table.setAttribute("border","1"); table.setAttribute("datasrc","#TEST"); var tbody = document.createElement("tbody"); var trs = new Array(); var tds = new Array(); TEST.Reset(); var rs = TEST.recordset; while (!rs.EOF){ var tbl = document.createElement("table"); table.setAttribute("border","1"); var td = document.createElement("td"); for(var i=1; i<2; i++){   var tr = document.createElement("tr");   trs[0] = document.createElement("tr");   trs[1] = document.createElement("tr");   trs[2] = document.createElement("tr"); tds[0] = document.createElement("td"); tds[1] = document.createElement("td"); tds[2] = document.createElement("td"); var img = document.createElement("img"); img.setAttribute("src",rs.fields(0)); img.setAttribute("alt",""); tds[0].appendChild(img); tds[1].innerText = rs.fields(1); tds[2].innerText = rs.fields(2); trs[0].appendChild(tds[0]); trs[1].appendChild(tds[1]); trs[2].appendChild(tds[2]); tr.appendChild(trs[0]); tr.appendChild(trs[1]); tr.appendChild(trs[2]); tbody.appendChild(tr); } rs.MoveNext; } table.appendChild(tbody); output.appendChild(table); } </script> </head> <body onload="hoge();"> CSV DATA IMPORT Ver MINO <p> <object id="TEST" classid="clsid:333C7BC4-460F-11D0-BC04-0080C7055A83"> <param name="DataURL" value="cn.csv" /> <param name="UseHeader" value="true" /> <param name="TextQualifier" value="'"> <param name="FieldDelim" value=","> </object> <div id="OUTPUT"> </div> </body> </html> ------------------------------------------------------------- javascriptを始めたばかりなので、まだよく理解していないので 大変申し訳ありませんが、ご指導願えればと思います。 よろしくお願いします。

専門家に質問してみよう