且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用javascript将项目添加到HTML下拉列表中

更新时间:2023-01-26 14:10:52

Since your script is in <head>, you need to wrap it in window.onload:

window.onload = function () {
    var select = document.getElementById("year");
    for(var i = 2011; i >= 1900; --i) {
        var option = document.createElement('option');
        option.text = option.value = i;
        select.add(option, 0);
    }
};

You can also do it in this way

<body onload="addList()">

相关阅读

技术问答最新文章