HTML DOM Input Radio checked 属性
htmlweb developmentfront end technology更新于 2024/5/18 5:00:17
HTML DOM Input Radio checked 属性与 radio 类型的 <input> 元素的 checked 属性关联。它主要用于设置或返回单选按钮的 checked 属性值。
语法
以下是语法 −
设置 checked 属性。
radioObject.checked = true|false
示例
让我们来看一个 Radio 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>
输出
这将产生以下输出 −

点击"勾选 Apple"按钮时,−

在上面的例子中,−
表单中有两个输入字段,它们具有共同的属性 type=“radio” 和 name=“fruits”。第一个单选按钮的 id 为 “Mango”,第二个单选按钮的 id 为 “Apple” −
<form> FRUIT: <input type=“radio” name=“fruits” id="Mango">Mango <input type="radio" name="fruits" id="Apple">Apple </form>
然后,我们创建了一个按钮"Check Apple",当用户点击该按钮时,它会执行 checkApple() 方法 −
<button type=”button” onclick="checkApple()">Check Apple</button>
checkApple() 方法使用 getElementById() 方法获取类型为 radio 的输入元素,并将其 checked 属性设置为 true。这将选中苹果单选按钮: −
function checkApple() {
document.getElementById("Apple").checked = true;
}
相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

