HTMLSelectElement.selectedIndex

您正在阅读此内容的英文版本,因为该语系尚未翻译。 帮助我们翻译此文章吧!

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

 

语法

var index = selectElem.selectedIndex;
selectElem.selectedIndex = index;

实例

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

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;
})

Specifications

Specification Status Comment
HTML Living Standard
HTMLSelectElement
Living Standard No change since the latest snapshot, HTML5.
HTML5
HTMLSelectElement
Recommendation Initial definition, snapshot of HTML Living Standard.

浏览器兼容性

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!
 
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support ? ? ? ? ?
Feature Android Chrome Firefox Mobile (Gecko) Firefox OS IE Phone Opera Mobile Safari Mobile
Basic support ? ? ? ? ? ? ?