Select dropdown menu option with javascript

前端 未结 2 1291
长发绾君心
长发绾君心 2021-02-05 02:30

I have a dropdown menu and I cannot figure out how to make a javascript function select a drop down menu option. I have tested the output of the variables and they are all corre

相关标签:
2条回答
  • 2021-02-05 03:11

    Change the line that reads:

    document.getElementById("stateSelect").selected = true;

    to:

    document.getElementById("stateSelect").selectedIndex = i;

    0 讨论(0)
  • 2021-02-05 03:12

    Alt. you can set selected to the actual option: select.options[i].selected = true;

    ...
            var select = document.getElementById("stateSelect");
            for(var i = 0;i < select.options.length;i++){
                if(select.options[i].value == c ){
                    select.options[i].selected = true;
                }
            }
    ...
    
    0 讨论(0)
提交回复
热议问题