-2

当 IE 加载页面时,我试图通过 JavaScript 添加一个选项。我在谷歌搜索它,但仍然无法得到解决方案。

<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 回答