简体   繁体   中英

element.click() not working on select element

If the try button is clicked should not the options be appeared?

 <select id='test1'> <option>Option_1</option> <option>Option_2</option> <option>Option_3</option> </select> <button onclick='test1();'>try</button> <script> function test1() { document.getElementById('test1').click(); } </script>

Unfortunately you can't do that. I suggest when you click the button, it changes to the next option.

function test1() {
    let el = document.getElementById('test1');
    let selectedIndex = el.selectedIndex;
    el.selectedIndex = selectedIndex + 1 === el.options.length ? 0 : selectedIndex + 1
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM