如果我知道下拉列表的ID,我就可以使用javascript来使用下拉菜单来选择一个下拉列表,但是如果该下拉列表没有ID,则无法选择它,所以我想知道是否可以选择页面上所有未使用ID的下拉列表?
document.getElementById("id").selectedIndex = 0;
To select all
const all = document.querySelectorAll('select');
To select the first
const first = document.querySelector('select');
console.log(first.selectedIndex);
edit:
Here you can see an example how too loop the multiple select-boxes and set the selectedIndex (in my case to 3)
const all = document.querySelectorAll('select'); [...all].forEach(select => select.selectedIndex = 3);
<select> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="opel">Opel</option> <option value="audi">Audi</option> </select> <select> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select>
all
is a NodeList and with [...all]
or optional Array.from(all)
you get an Array. This is needed to use the Array-Method forEach
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.