简体   繁体   English

如何使用JavaScript启用禁用多个DOM元素

[英]How to enable disable multiple DOM elements using Javascript

I'm trying to enable many radio buttons using DOM manipulation to avoid click each time on all buttons to enable them. 我正在尝试使用DOM操作来启用许多单选按钮,以避免每次都单击所有按钮以启用它们。

I try this: 我尝试这样:

document.getElementById("on").disabled = true; 

and: 和:

on-off-btn.off.active.setAttribute("enable", ""); 

Without success. 没有成功。 I think I'm doing wrong? 我想我做错了吗? My HTML looks like this: 我的HTML看起来像这样:

    <form>
    <div class="on-off-wrapper">
    <fieldset class="enabled">
    <div label="On" title="on" class="on-off-btn on active">
    <input type="radio" id="on" name="on_off" value="on"> 
    <span>On</span></div>
    <div label="Off" title="off" class="on-off-btn off">
    <input type="radio" id="off" name="on_off" value="on"> 
    <span>Off</span></div></fieldset></div>
    </form>

The code is always same about 60 time like this so this is why if I can enable all in one shot would be more simple. 这样的代码始终是大约60次相同,因此这就是为什么如果我能一次启用所有功能,则将变得更加简单。 I try this using the google dev tool with the console... 我尝试使用带有控制台的google dev工具...

If I understand your question correctly, multiple checkbox inputs in your HTML can be disabled and enabled via the following DOMElement methods; 如果我正确理解了您的问题,则可以通过以下DOMElement方法禁用和启用HTML中的多个checkbox输入;

// To set input disabled 
element.setAttribute("disabled", "disabled") 

// To set input enabled
element.removeAttribute("disabled") 

Combined with document.querySelectorAll() , you can achieve what you require as follows: document.querySelectorAll()结合使用,您可以实现所需的目标,如下所示:

 function disableAll() { // Select all inputs with name attribute of value on_off and iterate // applying disabled attribute document.querySelectorAll('input[name="on_off"]').forEach(element => { element.setAttribute("disabled", "disabled"); }); } function enableAll() { // Select all inputs with name attribute of value on_off and iterate // removing disabled attribute (to enable) document.querySelectorAll('input[name="on_off"]').forEach(element => { element.removeAttribute("disabled"); }); } 
 <form> <div class="on-off-wrapper"> <fieldset class="enabled"> <div label="On" title="on" class="on-off-btn on active"> <input type="radio" id="on" name="on_off" value="on"> <span>On</span></div> <div label="Off" title="off" class="on-off-btn off"> <input type="radio" id="off" name="on_off" value="on"> <span>Off</span></div> </fieldset> </div> </form> <button onclick="enableAll()">Enable All</button> <button onclick="disableAll()">Disable All</button> 

Update 更新资料

To achieve the updated toggle behavior mentioned in the comments below, see this: 要实现下面的注释中提到的更新的切换行为,请参阅以下内容:

 // Select all radio buttons document.querySelectorAll('input[type="radio"]').forEach(function(input) { // When any radio button is clicked input.addEventListener('click', function() { // 1. Reset all radio buttons to unchecked state document.querySelectorAll('input[type="radio"]') .forEach(function(reset) { reset.checked = false; }); // 2. Create a CSS selector to select all radio buttons that match the .on or .off // parent of the current radio button being clicked const selector = (input.parentElement.classList.contains('on') ? '.on' : '.off') + ' input[type="radio"]'; // 3. Select all radio buttons by selector (ie those that match this radio buttons // .on or .off parent), and set to checked document.querySelectorAll(selector).forEach(function(set) { set.checked = true; }); }) }); 
 <form> <div class="on-off-wrapper"> <fieldset class="enabled"> <div label="On" title="on" class="on-off-btn on active"> <input type="radio" id="on" name="on_off" value="on"> <span>On</span></div> <div label="Off" title="off" class="on-off-btn off"> <input type="radio" id="off" name="on_off" value="on"> <span>Off</span></div> </fieldset> </div> </form> <form> <div class="on-off-wrapper"> <fieldset class="enabled"> <div label="On" title="on" class="on-off-btn on active"> <input type="radio" id="on" name="on_off" value="on"> <span>On</span></div> <div label="Off" title="off" class="on-off-btn off"> <input type="radio" id="off" name="on_off" value="on"> <span>Off</span></div> </fieldset> </div> </form> <form> <div class="on-off-wrapper"> <fieldset class="enabled"> <div label="On" title="on" class="on-off-btn on active"> <input type="radio" id="on" name="on_off" value="on"> <span>On</span></div> <div label="Off" title="off" class="on-off-btn off"> <input type="radio" id="off" name="on_off" value="on"> <span>Off</span></div> </fieldset> </div> </form> <form> <div class="on-off-wrapper"> <fieldset class="enabled"> <div label="On" title="on" class="on-off-btn on active"> <input type="radio" id="on" name="on_off" value="on"> <span>On</span></div> <div label="Off" title="off" class="on-off-btn off"> <input type="radio" id="off" name="on_off" value="on"> <span>Off</span></div> </fieldset> </div> </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM