Latest web development tutorials

HTML DOM Radio checked 屬性

Radio 對象參考手冊 Radio對象

定義和用法

checked 屬性可設置或返回某個單選按鈕是否被選中。

語法

設置checked 屬性:

radioObject.checked=true|false

返回checked 屬性:

radioObject.checked


瀏覽器支持

Internet ExplorerFirefoxOperaGoogle ChromeSafari

所有主要瀏覽器都支持checked 屬性


實例

實例

下面的例子可對一個單選按鈕進行選定和不選定:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>本教程(w3big.com)</title>
<script>
function check(){
    document.getElementById("red").checked=true
}
function uncheck(){
    document.getElementById("red").checked=false
}
</script>
</head>
<body>

<form>
你更喜欢哪种颜色?<br>
<input type="radio" name="colors" id="red">红色<br>
<input type="radio" name="colors" id="blue">蓝色<br>
<input type="radio" name="colors" id="green">绿色
</form>
<button type="button" onclick="check()">选择 "红色"</button>
<button type="button" onclick="uncheck()">不选择 "红色"</button>
    
</body>
</html>

嘗試一下»


Radio 對象參考手冊 Radio對象