HTMLSelectElement.selectedIndex

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

HTMLSelectElement.selectedIndex 是一个长整型数,它反映了被选中的第一个 <option> 元素的索引值。值为 -1 时表明没有元素被选中。

一个数字。

示例

HTML

html
<p id="p">selectedIndex: 0</p>

<select id="select">
  <option selected>Option A</option>
  <option>Option B</option>
  <option>Option C</option>
  <option>Option D</option>
  <option>Option E</option>
</select>

JavaScript

js
var selectElem = document.getElementById("select");
var pElem = document.getElementById("p");

// 当有新的<option>元素被选中时
selectElem.addEventListener("change", function () {
  var index = selectElem.selectedIndex;
  // 把 index 数据添加到 p 元素中
  pElem.innerHTML = "selectedIndex: " + index;
});

规范

Specification
HTML
# dom-select-selectedindex-dev

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
selectedIndex

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support