set option “selected” attribute from dynamic created option

后端 未结 17 1703
时光说笑
时光说笑 2020-11-30 20:01

I have a dynamically created select option using a javascript function. the select object is


<         


        
17条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 21:00

    To set the input option at run time try setting the 'checked' value. (even if it isn't a checkbox)

    elem.checked=true;
    

    Where elem is a reference to the option to be selected.

    So for the above issue:

    var country = document.getElementById("country");
    country.options[country.options.selectedIndex].checked=true;
    

    This works for me, even when the options are not wrapped in a .

    If all of the tags share the same name, they should uncheck when the new one is checked.

提交回复
热议问题