簡體   English   中英

錯誤在哪里[JS DOM HTML]?

[英]Where is the error [JS DOM HTML]?

我想使用js函數向select添加選項。 但是媒介上有一個問題,我認為有人可以幫助我嗎?

<html>
<body onload="myFunction()">
<select id="mySelect"></select>
<script>
    function myFunction() {
        var months= ["january","february"];
        var mySelect = document.getElementById('mySelect');
        var newOption = document.createElement('option');
        for(var i=0;i<mesi.length;i++){
             newOption.innerHtml=mesi(i).valueOf;
             mySelect.appendChild(newOption);
        }
    }
</script>
</body>
</html>

您的代碼似乎在for塊中具有錯誤的數組名稱。 另外,當您在數組中調用索引時,請使用方括號-months months[i]而不是括號。 並且不需要使用valueOf。

    function myFunction() {
var months= ["january","february"];
var mySelect = document.getElementById('mySelect');
var newOption = document.createElement('option');
for(var i=0;i<months.length;i++){ // <- here is where the fix goes.
     newOption.innerHtml=months[i];// <- here is where the fix goes.
     mySelect.appendChild(newOption);
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM