-2

IE がページをロードするときに、JavaScript を介してオプションを追加しようとしています。Googleで検索しましたが、まだ解決できません。

<html>
    <script type="text/javascript">
        function add(){
            var c = document.getElementById("number");
            var e = document.createElement("option");
            e.setAttribute("value", "1");
            e.appendChild(document.createTextNode("two");
            c.appendChild(e);
        }
    </script>
<body onload="add()">
    <form action="" method="post">
        favirate city:
        <select id="number">
            <option value="0">one</option>
        </select>
    </form>
</body>
</html> 
4

2 に答える 2

0
var option = new Option('myText', 'val');
var select = document.getElementById("number");
select.appendChild(option);
于 2013-07-30T22:28:53.113 に答える
-1

このコードを試してください...少し更新しました

<html>
    <script type="text/javascript">
        function add(){
            var c = document.getElementById("number");
            var lastVal = Number(c.lastChild.value) + 1;
            var e = document.createElement("option");
            e.setAttribute("value", lastVal);
            e.appendChild(document.createTextNode("two"));
            c.appendChild(e);
        }
    </script>
<body onload="add()">
    <form action="" method="post">
        favirate city:
        <select id="number">
            <option value="0">one</option>
        </select>
    </form>
</body>
</html>
于 2013-07-30T22:32:53.213 に答える