HTMLOptionsCollection: selectedIndex property
Thuộc tính selectedIndex của giao diện HTMLOptionsCollection là chỉ số số của phần tử <option> đầu tiên được chọn, nếu có, hoặc -1 nếu không có <option> nào được chọn. Đặt thuộc tính này chọn tùy chọn tại chỉ số đó và bỏ chọn tất cả các tùy chọn khác trong tập hợp này, trong khi đặt nó thành -1 bỏ chọn bất kỳ phần tử nào hiện đang được chọn. Nó hoàn toàn tương đương với thuộc tính selectedIndex của HTMLSelectElement sở hữu tập hợp này.
Giá trị
Một số.
Ví dụ
js
const optionColl = document.getElementById("select").options;
console.log(`selected option: ${optionColl.selectedIndex}`); // the index of the first selected option, or -1 if no option is selected
optionColl.selectedIndex = 0; // selects the first item
optionColl.selectedIndex = -1; // deselects any selected option
Thông số kỹ thuật
| Specification |
|---|
| HTML> # the-htmloptionscollection-interface:dom-htmloptionscollection-selectedindex-2> |