Is it possible to use a for loop in <select> in html? and how?

后端 未结 6 1563
不知归路
不知归路 2021-01-05 20:38

I am trying to use a for loop in html but i dont even know if this is possible. Is it? and if yes how? I dont want to use php. only html and javascript.

this is my

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-05 21:13

    One way is to use DynamicHTML. Let the html page have a place holder for the options of select tag.

    
    

    In a js file

    var options = ["one","two","three"], selectHtml = "";
    
    for(var optionIndex = 0; optionIndex < options.length; optionIndex++) {
    
        selectHtml += ("");
    
    }
    
    document.getElementById("selectBox").innerHTML = selectHtml;
    

    Put the above code in a function and call that function onload.

提交回复
热议问题