Change the options array of a select list

前端 未结 5 1311
青春惊慌失措
青春惊慌失措 2020-12-03 10:29

Is there a way to change the options array of an html select list using javascript or mootools?

I need to replace the entire options set with a new one. In my ajax

5条回答
  •  清歌不尽
    2020-12-03 10:34

    Vanilla JS solution

    var arrOptions = [];
    arrOptions.push("");
    arrOptions.push("");
    arrOptions.push("");
    
    document.getElementById("selectInput").innerHTML = arrOptions.join();
    

    If you already have a set of options

    var arrOptions = [];
    var arrOptionsCollection = document.getElementById("check_typeInput").options;
    for (var i=0, n = arrOptionsCollection.length; i < n; i++) { // looping over the options
        if (arrOptionsCollection[i].value) {
            arrOptions.push("");
        }
    }
    arrOptions.push("");
    arrOptions.push("");
    
    document.getElementById("selectInput").innerHTML = arrOptions.join();
    

提交回复
热议问题