defaultselected
Option defaultSelected 属性
定义和用法
defaultSelected 属性可返回 selected 属性的初始值。
如果选择了默认的选项,则返回 true,否则返回 fasle。
如果表单被重置,则 selected 属性重置为该属性的值。这个该属性也会设置 selected 属性的值。
注意: 默认的选项将显示在第一位。
语法
optionObject.defaultSelected
浏览器支持
所有主要浏览器都支持 defaultSelected 属性
实例
下面的例子可检测所选项是否是默认选项:
<html>
<head>
<script>
function displayResult()
{
var x=document.getElementById("mySelect").selectedIndex;
var y=document.getElementsByTagName("option");
alert("Is " + y[x].text + " selected by default? " + y[x].defaultSelected);
}
</script>
</head>
<body>
<form>
Select your favorite fruit:
<select id="mySelect">
<option>Apple</option>
<option>Orange</option>
<option selected="selected">Pineapple</option>
<option>Banana</option>
</select>
</form>
<button type="button" onclick="displayResult()">Is selected fruit selected by default?</button>
</body>
</html>
<head>
<script>
function displayResult()
{
var x=document.getElementById("mySelect").selectedIndex;
var y=document.getElementsByTagName("option");
alert("Is " + y[x].text + " selected by default? " + y[x].defaultSelected);
}
</script>
</head>
<body>
<form>
Select your favorite fruit:
<select id="mySelect">
<option>Apple</option>
<option>Orange</option>
<option selected="selected">Pineapple</option>
<option>Banana</option>
</select>
</form>
<button type="button" onclick="displayResult()">Is selected fruit selected by default?</button>
</body>
</html>