是否可以在html中的<select>中使用for循环?如何?

use*_*482 5 html javascript

我试图在html中使用for循环,但我甚至不知道这是否可行.是吗?如果是的话怎么样?我不想使用PHP.只有HTML和JavaScript.

这是我的目标:我有一个包含.txt文件的文件.我想计算txt文件的数量,当我得到我希望将它发送到我将使用for循环将txt文件的数字放入dropbox的数字.

谢谢

rle*_*mon 14

很多答案....这里是另一种使用document.write或innerHTML或jQuery ....

HTML

<select id="foo"></select>
Run Code Online (Sandbox Code Playgroud)

JS

(function() { // don't leak
    var elm = document.getElementById('foo'), // get the select
        df = document.createDocumentFragment(); // create a document fragment to hold the options while we create them
    for (var i = 1; i <= 42; i++) { // loop, i like 42.
        var option = document.createElement('option'); // create the option element
        option.value = i; // set the value property
        option.appendChild(document.createTextNode("option #" + i)); // set the textContent in a safe way.
        df.appendChild(option); // append the option to the document fragment
    }
    elm.appendChild(df); // append the document fragment to the DOM. this is the better way rather than setting innerHTML a bunch of times (or even once with a long string)
}());
Run Code Online (Sandbox Code Playgroud)

这是一个演示它的小提琴.