Html 使用javascript将项目添加到html中的下拉列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18417114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 12:32:39  来源:igfitidea点击:

add item to dropdown list in html using javascript

javascripthtml

提问by Oyindamola 'Funmi Oni

I have this javascript+html to populate a dropdown menu but it is not working, am i doing anything wrong? Note I want the drop down menu to be filled on page Load

我有这个 javascript+html 来填充下拉菜单,但它不起作用,我做错了什么吗?注意我希望在页面加载时填充下拉菜单

    <!DOCTYPE html>
    <html>
    <head>
    <script>
    function addList(){
    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);
      }
     }
    </script>
    </head>

    <body>

       <select id="year" name="year"></select>

    </body>
    </html> 

回答by Kabie

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

由于您的脚本在 中<head>,因此您需要将其包装在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()">

回答by Stefan Steiger

For higher performance, I recommend this:

为了获得更高的性能,我建议这样做:

var select = document.getElementById("year");
var options = [];
var option = document.createElement('option');

//for (var i = 2011; i >= 1900; --i)
for (var i = 1900; i < 2012; ++i)
{
    //var data = '<option value="' + escapeHTML(i) +'">" + escapeHTML(i) + "</option>';
    option.text = option.value = i;
    options.push(option.outerHTML);
}

select.insertAdjacentHTML('beforeEnd', options.join('\n'));

This avoids a redraw after each appendChild, which speeds up the process considerably, especially for a larger number of options.

Optional for generating the string by hand:

这避免了在每个 appendChild 之后重绘,这大大加快了进程,特别是对于大量选项。

可选用于手动生成字符串:

function escapeHTML(str)
{
    var div = document.createElement('div');
    var text = document.createTextNode(str);
    div.appendChild(text);
    return div.innerHTML;
}

However, I would not use these kind of methods at all.
It seems crude. You best do this with a documentFragment:

但是,我根本不会使用这些方法。
看起来很粗糙。您最好使用 documentFragment 执行此操作:

var docfrag = document.createDocumentFragment();

for (var i = 1900; i < 2012; ++i)
{
     docfrag.appendChild(new Option(i, i));
}

var select = document.getElementById("year");
select.appendChild(docfrag);

回答by Alex

Try to use appendChildmethod:

尝试使用appendChild方法:

select.appendChild(option);

回答by Golda

Try this

尝试这个

<script type="text/javascript">
    function AddItem()
    {
        // Create an Option object       
        var opt = document.createElement("option");        

        // Assign text and value to Option object
        opt.text = "New Value";
        opt.value = "New Value";

        // Add an Option object to Drop Down List Box
        document.getElementById('<%=DropDownList.ClientID%>').options.add(opt);
    }
<script />

The Value will append to the drop down list.

该值将附加到下拉列表中。

回答by mithunsatheesh

i think you have only defined the function. you are not triggering it anywhere.

我想你只定义了函数。你不会在任何地方触发它。

please do

请做

window.onload = addList();

or trigger it on some other event

或在其他事件上触发它

after its definition

在它的定义之后

see this fiddle

看到这个小提琴