通过js控制radio单选框的disabled属性,可以直接操作dom元素的disabled属性,将其设置为true或false。例如,使用document.getelementbyid('radioid').disabled = true;可以禁用指定的radio按钮,而设置为false则可以启用它。需要注意的是,disabled属性会影响表单提交,被禁用的元素不会被提交。

如何通过ID获取并禁用特定的radio按钮
可以通过document.getElementById方法获取特定ID的radio按钮,然后设置其disabled属性为true来禁用它。这种方法适用于已知radio按钮ID的场景。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Radio Disabled Example</title>
</head>
<body>
<input type="radio" id="radio1" name="group1" value="1"> Option 1
<input type="radio" id="radio2" name="group1" value="2"> Option 2
<button onclick="disableRadio()>Disable Radio 1</button>
<script>
function disableRadio() {
document.getElementById('radio1').disabled = true;
}
</script>
</body>
</html>
如何批量禁用同一组的所有radio按钮
可以使用document.getElementsByName方法获取同一组的所有radio按钮,然后遍历它们并设置disabled属性为true。这种方法适用于需要一次性禁用多个radio按钮的场景。
立即学习“前端免费学习笔记(深入)”;

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Batch Disable Radio Example</title>
</head>
<body>
<input type="radio" name="group1" value="1"> Option 1
<input type="radio" name="group1" value="2"> Option 2
<input type="radio" name="group1" value="3"> Option 3
<button onclick="batchDisable()>Disable All in Group</button>
<script>
function batchDisable() {
var radios = document.getElementsByName('group1');
for (var i = 0; i < radios.length; i++) {
radios[i].disabled = true;
}
}
</script>
</body>
</html>
禁用radio后如何重新启用
要重新启用被禁用的radio按钮,只需将disabled属性设置为false即可。这可以通过直接操作DOM元素或使用事件监听器来实现。



















