【问题标题】:Add a class to a specific checkbox when is checked选中时将类添加到特定复选框
【发布时间】:2021-08-21 06:25:15
【问题描述】:

我有一个 foreach 回显,它在我的网站上显示项目列表,我想创建一个系统来添加绿色背景(选中复选框时)并在不再选中时删除该复选框。

echo '<form action="selector.php" id="form1" method="post" target="_self">';

foreach ($Query as $row) {  
  echo '
    <div class="item">
      <img src="something.png">
                            
      <label class="containerPen" style="color: #00ff00">
        Accept                              
        <input type="checkbox"  name="accepted[]" value="'.$row['topic_identification'].'" id="accepted"></input>
        </br>
        <span class="checkmarkPen"></span>
      </label>
                        
      <label class="containerPenX" style="color: #ff0000">
        Decline
        <input type="checkbox" name="declined[]" value="'.$row['topic_identification'].'" id="declined"></input>
        <span class="checkmarkPenX"></span>
      </label>
    </div>          
  ';
}

echo '
  <input type="submit" name="submit" value="submit"> 

  </form>
';

对于每个复选框,我想添加类

.greenbox {
  background-color: green;
}

当它被选中时,当我取消选中它时,我希望它删除该类。

谢谢!

这是 ContainerPen 和 CheckmarkPen 类的 CSS

    .containerPen {
  display: block;
  position: relative;
  padding-left: 35px;
  margin-bottom: 12px;
  cursor: pointer;
  font-size: 22px;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}


.containerPen input {
  position: absolute;
  opacity: 0;
  cursor: pointer;
  height: 0;
  width: 0;
}


.checkmarkPen {
  position: absolute;
  top: 0;
  left: 0;
  height: 25px;
  width: 25px;
  background-color: #eee;
}


.containerPen:hover input ~ .checkmarkPen {
  background-color: #00ff00;
}


.containerPen input:checked ~ .checkmarkPen {
  background-color: #00ff00;

}


.checkmarkPen:after {
  content: "";
  position: absolute;
  display: none;
}


.containerPen input:checked ~ .checkmarkPen:after {
  display: block;
}


.containerPen .checkmarkPen:after {
  left: 9px;
  top: 5px;
  width: 7px;
  height: 12px;
  border: solid white;
  border-width: 0 3px 3px 0;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
}

【问题讨论】:

    标签: javascript php css checkbox


    【解决方案1】:
    $onload = <<<EOT
     <script>    
        window.onload = function() {
           const chk = document.querySelectorAll('input[type="checkbox"]');
           for(let x=0; x< chk.length; x++) {
            chk[x].addEventListener('change', function (e) {
              if (e.target.checked) e.target.classList.add('greenbox');
              else e.target.classList.remove('greenbox')
           })
          }
        }
    </script>
    
    EOT;
    
    // then add it where you want.
    
    echo $onload;
    

    重要 这种回声称为Heredoc,有两个要求(否则会出错)。初始&lt;&lt;&lt;EOT 之后不能有空格(只是一个换行符),结束EOT; 之前不能有空格。事实上,最后一行只能EOT;,只要确定空格即可。否则,这是为 html 和 javascript 块等内容形成可读 php 输出的好方法。

    【讨论】:

    • 我试过用这个,但我不明白如何使用 $onload =
    • 是的,heredoc 很挑剔。我用关于使用的注释更新了我的答案。你只需要做一个小的调整
    【解决方案2】:

    您可以在将 id 传递给函数的两个复选框上添加 eventListener 或 onclick 事件。

    然后你可以使用 JavaScript 的内置 classlist 属性来添加和删除类。

    const addClass = (id) => {
        let checkbox = document.querySelector(`#${id}`);
        checkbox.classList.toggle('greenbox');
    }
    .greenbox{
      background-color: green;
    }
    <input type="checkbox" onclick="addClass(this.id)" id="accepted">    
    <input type="checkbox" onclick="addClass(this.id)" id="declined">

    【讨论】:

    • 您好!我试过了,它正在将绿盒添加到输入中,我需要将它添加到标签中。我试图将 onclick 函数移到标签上,但没有任何反应...
    • 在标签使用“checkbox.parentElement.classList.toggle('greenbox')”的情况下,这会将类应用于输入的父元素,在您的情况下是标签
    • 工作,但不正确。当我选中任何复选框时,它只选择第一个 imgur.com/a/XXdeX51
    • 这可能是因为您可能正在重用元素ids。它们应该是独一无二的。
    【解决方案3】:

    作为替代方法;

    根据您支持的浏览器,您可以通过 CSS :checked 伪类选择器简单地做您想做的事情。

    您可以阅读它here 并查看兼容性here

    用法示例是;

    input:checked
    {
        background-color: green;
    }
    

    或者类似的东西;

    label input:checked
    {
        background-color: green;
    }
    

    如果您只想让它影响标签内的输入。请注意,此选择器也将应用于选择中的单选按钮或选项。

    【讨论】:

    • 它们都不起作用...我忘记为标签和跨度添加 CSS。我编辑了主要帖子。我真的很想用 CSS 解决这个问题,但到目前为止我试过它没有用。 :C
    • 我将您在帖子中提供的 HTML 和 CSS 复制到了 this JSFiddle 并且单击“接受”“复选框”时它似乎变成了绿色。您的预期结果究竟是什么?
    • imgur.com/a/XXdeX51 我需要标签变成绿色,而不是复选框。我将 CSS 添加到主帖中。
    • 所以您希望复选框的 父元素 具有绿色背景,而不是复选框本身?在这种情况下......目前通过 CSS 设置的当前 HTML 是不可能的,因为您无法通过 CSS 选择器选择父元素或上一个元素。有关更多信息,请参阅this 页面。看起来 JavaScript 是您目前唯一的解决方案。
    猜你喜欢
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-26
    • 1970-01-01
    • 1970-01-01
    • 2017-10-15
    • 2017-02-07
    相关资源
    最近更新 更多