我是如此接近!

我希望选中该复选框时,两个单选按钮都将启用.未选中时,两个单选按钮都被禁用.

请不要Jquery

JS公司:

var sipch = document.querySelector("input[name=sip]");
sipch.addEventListener("change", function (event) {
    if (event.currentTarget.checked) {
        document.querySelector("input[name=protocol]").removeAttribute("disabled");
    } else {
        document.querySelector("input[name=protocol]").setAttribute("disabled", "disabled");
    }
});

HTML:

<!DOCTYPE html>
<html lang="en">
    <body>
        <form id="myForm" onsubmit="goPVFive(event)" method="get">
            <div id="pBFContainer" class="container">
                <div id="bodyFOption1">
                    <input type="checkbox" name="sip" value="true">Would you like to include establishing the SIP session test?<p>
                    <input type="radio" class="testD" name="protocol" value="udp" disable/>UDP
                    <input type="radio" class="testD" name="protocol" value="tcp" disable/>TCP <label class="testD">(UDP is most Common)</label>
                </div>
                <div id="bodyFOption2">
                    <input type="checkbox" name="fWallInt" value="true">Would you include SIP ALG firewall interference test"
                    </div>
            </div>
            <input type="submit" id="subButton" value="Next..." />
        </form>
    </body>
    <script type="text/javascript" src="evalportalp1.js"></script>
</html>

推荐答案

您需要使用querySelectorAll,然后循环查看结果,禁用/启用其中的每一项:

  if (event.currentTarget.checked) {
    document.querySelectorAll('input[name=protocol]')
        .forEach((elem) => elem.removeAttribute('disabled'));
  } else {
    document.querySelectorAll('input[name=protocol]')
        .forEach((elem) => elem.setAttribute('disabled', 'disabled'));
  }

Javascript相关问答推荐

函数返回与输入对象具有相同键的对象

阿波罗返回的数据错误,但在网络判断器中是正确的

如何在 cypress 中使用静态嵌套循环

在Three JS中看不到补间不透明度更改效果

正则表达式,允许我匹配除已定义的子字符串之外的所有内容

OpenAI转录API错误请求

如何在Press上重新启动EXPO-AV视频?

未捕获的运行时错误:调度程序为空

我怎样才能点击一个元素,并获得一个与 puppeteer 师导航页面的URL?

如何在移动设备中使用JAVASSCRIPT移除点击时的焦点/悬停状态

连续添加promise 时,如何在所有promise 都已结算时解除加载覆盖

在Press Reaction本机和EXPO av上播放单个文件

将以前缓存的 Select 器与querySelector()一起使用

使用Java脚本筛选数组中最接近值最小的所有项

如何从图表中映射一组图表-js使用REACT

如何将PNG图像放在wix站点的Open Streemap map 上,使PNG图像成为 map 不可分割的一部分?

如何使用属性访问器定义循环的for...的局部变量

查找函数句柄的模块/文件

类型脚本类型声明未按预期工作

UseEffectreact 18-花很长时间进行API调用