Select selectedIndexプロパティ

Select 对象参考手册Select オブジェクト

定義と用法

selectedIndex プロパティは、ドロップダウンリストで選択されたオプションのインデックス番号を設定または返します。

注意:複数選択が許可されている場合、最初に選択されたオプションのインデックス番号のみが返されます。

構文

selectedIndex プロパティを設定する:

selectObject.selectedIndex=integer

selectedIndex プロパティを返す:

selectObject.selectedIndex


ブラウザのサポート

Internet ExplorerFirefoxOperaGoogle ChromeSafari

すべての主要ブラウザは selectedIndex プロパティをサポートしています。


例

例

以下の例では、選択されたオプションのインデックス番号を表示します:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>サンプル(example.com)</title>
<script>
function displayResult(){
    var x=document.getElementById("mySelect").selectedIndex;
    var y=document.getElementById("mySelect").options;
alert("索引: " + y[x].index + " is " + y[x].text);
}
</script>
</head>
<body>

<form>
あなたが一番好きな果物を選択してください:
<select id="mySelect">
    <option>Apple</option>
    <option>Orange</option>
    <option>Pineapple</option>
    <option>Banana</option>
</select>
</form>
<button type="button" onclick="displayResult()">インデックスを表示</button>

</body>
</html>

試してみる »


Select 对象参考手册Select オブジェクト その他の拡張