簡體   English   中英

JavaScript - 啟用/禁用單選按鈕的復選框

[英]JavaScript - Checkbox to enable/disable radio buttons

我很近! 默認情況下,按鈕處於啟用狀態。 同樣,在檢查兩者時,都將啟用兩者,但是如果未選中,則僅禁用一個。

我想要它,以便在選中復選框時啟用兩個單選按鈕。 未選中時,兩個單選按鈕都被禁用。

沒有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'));
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM