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

时间:2019-01-10 23:51:15

标签: javascript dom radio-button

我正在尝试使用DOM操作启用许多单选按钮,以避免每次都单击所有按钮以启用它们。

我尝试:

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

和:

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

没有成功。我想我做错了吗?我的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>

这样的代码总是大约60次,所以这就是为什么如果我能一次启用所有功能,就会更简单。我可以在控制台上使用google dev工具尝试此操作...

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,则可以通过以下DOMElement方法禁用和启用HTML中的多个checkbox输入;

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

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

结合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>

更新

要实现以下注释中提到的更新的切换行为,请参见:

// 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>