JavaScript to create a dropdown list and get the selected value

前端 未结 2 1231
天命终不由人
天命终不由人 2020-12-16 23:35

I need to add a dropdown list to a webpage. generally the HTML code is like this;


                        
    
提交评论

  • 2020-12-16 23:53
    var select = document.createElement("select");
    select.id = "au_1_sel";
    select.name="au_1_sel";
    select.class=class="searchw8";
    
    var option1 = document.createElement("option");
    option.value="AU";
    option.selected="";
    option.innerHTML= "method1";
    
    var option2 = document.createElement("option");
    option.value="FI";
    option.innerHTML= "method2";
    
    select.addChild(option1);
    select.addChild(option2);
    document.addChild(select);
    
    var button = document.getElementById("button1");
    button.onClick=function(){alert(select.options[select.selectedIndex].value);}
    
    0 讨论(0)
  • 提交回复
    热议问题