【问题标题】:Change background color of inputs if checked or not如果选中,则更改输入的背景颜色
【发布时间】:2021-02-01 05:17:22
【问题描述】:

我正在尝试写一些内容,如果选中输入复选框,则更改该输入框的相关标签的背景颜色。如果选中,则应为红色,否则所有其他都具有其常规颜色。当我单击其他输入时,如何取消选中其他输入?所有实际输入都显示为无,所以我想更改标签的样式。


document.querySelectorAll(".main-container__checkbox").forEach((item) => {
  if (item.checked) {
    document.querySelectorAll(
      ".main-container__checkbox-label"
    ).style.backgroundColor = "red";
  }
});

显然这只会让每个标签的背景颜色变成红色,但我不知道如何让被选中的标签变成红色

 <input
          id="main-checkbox-1"
          type="checkbox"
          class="main-container__checkbox"
          checked
        />
        <input
          id="main-checkbox-2"
          type="checkbox"
          class="main-container__checkbox"
        />
        <input
          id="main-checkbox-3"
          type="checkbox"
          class="main-container__checkbox"
        />

       
          <label for="main-checkbox-1" class="main-container__checkbox-label"
            >&nbsp;</label
          >
          <label for="main-checkbox-2" class="main-container__checkbox-label"
            >&nbsp;</label
          >
          <label for="main-checkbox-3" class="main-container__checkbox-label"
            >&nbsp;</label
          >

【问题讨论】:

    标签: javascript html


    【解决方案1】:

    如果您修改您的 html 以使标签环绕输入 - 然后将文本放在一个跨度中 - 在输入复选框之后,那么您可以单独使用 css 来实现这一点 - 使用 :checked 伪选择器和“ +" 直接同级组合子。

    这有一个额外的好处是可以点击标签文本并切换复选框的选中状态。

    然后您可以更进一步,隐藏复选框输入并将标签样式设置为看起来像一个按钮,这样您就有了一个功能齐全且有效的切换开关。

    编辑 - 请注意在原始帖子中 - 您提到输入是“显示:无”,然后我遵循了这一点,并使这些输入像切换按钮一样。

    label {
      display: block;
      padding: 10px;
      
    }
    
    label input {
     display: none;
    }
    .checkbox-label {
      background-color: #fff;
      color: #222;
      padding: 5px 10px;
      cursor: pointer;
      border: solid 1px #6e6e6e;
      border-radius: 4px;
      margin-bottom: 10px
    }
    
    input:checked + .checkbox-label {
      background-color: red;
      color: #fff;
    }
    <p> click the following to toggle the hidden checkboxes - if the checkbox is checked - the label background will be red <p>
    
    <label>
       <input
        id="main-checkbox-1"
        type="checkbox"
        class="main-container__checkbox"
        checked
        />
        <span class="checkbox-label">main-checkbox-1</span>
     </label>
      
     <label>
       <input
        id="main-checkbox-2"
        type="checkbox"
        class="main-container__checkbox"
        />
        <span class="checkbox-label">main-checkbox-2</span>
     </label>
     
      <label>
       <input
        id="main-checkbox-3"
        type="checkbox"
        class="main-container__checkbox"
        />
        <span class="checkbox-label">main-checkbox-3</span>
     </label>

    【讨论】:

    • 感谢您的好回答,但我认为在 javascript 中确实需要它,因为我只需要一次可以检查一个输入,并且还希望根据输入在 html 中显示不同的内容被检查。只能用js解决吗?
    • 如果一次只能选择一个输入 - 那么您需要的是单选按钮组而不是复选框组 - 单选按钮和复选框之间的根本区别是一次只能选择一个单选按钮可以选择 - 而多个复选框可以一次选择。你仍然可以使用 css 创建这个 - 并且不同的选择器会给出不同的背景颜色。如果可能,CSS 总是更可取的性能。
    • 这就是为什么这对我来说很难弄清楚,我使用复选框而不是单选按钮......完全忘记了它们并试图使用复选框来解决这个问题,我一直在失去理智.非常感谢。
    【解决方案2】:

    使用 javascript,您可以使用事件侦听器实现相同的目的。添加了内联 cmets 以便于理解

    // getting all the checkbox , iterating to add event listener 
    document.querySelectorAll(".main-container__checkbox").forEach((item) => {
      // calling the function to pass the current element under iteration
      addBackGround(item);
      // adding event listener to the checkbox so on change it will add/remove the background color
      item.addEventListener('change', function(e) {
        // e is event object, e.target is the element on which any event occured
        addBackGround(e.target)
      })
    
    })
    // a function which will add the back ground color
    function addBackGround(item) {
      // getting the id of the element 
      const elemId = item.getAttribute('id');
      // finding the label using attribute selector
      const labelElement = `label[for=${elemId}]`;
      // if checkbox is checked then add background color
      if (item.checked) {
        document.querySelector(labelElement).style.backgroundColor = "red";
      } else {
        document.querySelector(labelElement).style.backgroundColor = "";
      }
    }
    <input id="main-checkbox-1" type="checkbox" class="main-container__checkbox" checked />
    <label for="main-checkbox-1" class="main-container__checkbox-label">
    main-checkbox-1
    </label>
    <input id="main-checkbox-2" type="checkbox" class="main-container__checkbox" />
    <label for="main-checkbox-2" class="main-container__checkbox-label">main-checkbox-</label>
    <input id="main-checkbox-3" type="checkbox" class="main-container__checkbox" />
    <label for="main-checkbox-3" class="main-container__checkbox-label">main-checkbox-3</label>

    【讨论】:

      猜你喜欢
      • 2021-03-07
      • 1970-01-01
      • 2021-12-06
      • 2014-05-17
      • 1970-01-01
      • 2014-12-25
      • 2017-02-21
      • 2022-01-06
      • 1970-01-01
      相关资源
      最近更新 更多