HTML DOM 输入单选按钮 checked 属性
HTML DOM Input 单选按钮 checked 属性与 type 为 radio 的 <input> 元素的 checked 属性相关联。它用于设置或返回单选按钮的 checked 属性值。
语法
以下是语法 −
设置 checked 属性。
radioObject.checked = true|false
示例
让我们看一个单选按钮 checked 属性的示例 −
<!DOCTYPE html>
<html>
<body>
<h1>Input Radio checked property</h1>
<form>
FRUIT:
<input type="radio" name="fruits" id="Mango">Mango
<input type="radio" name="fruits" id="Apple">Apple
</form>
<br><button type=”button” onclick="checkApple()">Check Apple</button>
<script>
function checkApple() {
document.getElementById("Apple").checked = true;
}
</script>
</body>
</html>输出
将产生以下输出 −

单击“选中苹果”按钮 −

在上述示例中 −
在一个表格中包含两个具有 type=“radio” 和 name=“fruits” 的公共属性的输入字段。第一个单选按钮的 id 为“芒果”,第二个的 id 为“苹果” −
<form> FRUIT: <input type="radio" name="fruits" id="Mango">Mango <input type="radio" name="fruits" id="Apple">Apple </form>
然后创建一个“选中苹果”按钮,用户单击该按钮时将执行 checkApple() 方法 −
<button type=”button” onclick="checkApple()">Check Apple</button>
checkApple() 方法使用 getElementById() 方法获取类型为单选按钮的输入元素,并将其 checked 属性设置为 true。这将选中苹果单选按钮: −
function checkApple() {
document.getElementById("Apple").checked = true;
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程语言
C++
C#
MongoDB
MySQL
Javascript
PHP