【问题标题】:Change color of a styled checked checkbox with button使用按钮更改样式选中复选框的颜色
【发布时间】:2017-10-18 02:05:15
【问题描述】:

我已经坚持了一段时间了。为了快速解释,我有一张不同复选框的表格。用户选中许多框并继续单击。同一个表格显示在另一个页面上/被选中的框应该通过单击按钮来加载。

我的问题是,如何通过单击按钮将样式复选框的颜色更改为另一种颜色。

更新,我已经添加了代码,很抱歉造成混乱,第一次使用这个

table.example1, table.example1 th, table.example1 td {
    border: 3px solid grey;
    max-width: 200px;
}

input[type=checkbox] {
    display:none;
}

input[type=checkbox] + label {
    display:inline-block;
    padding: 6px 60px;
    margin-bottom: 0;
    font-size: 14px;
    line-height: 20px;
    color: white;
    text-align: center;
    vertical-align: middle;
    cursor: pointer;
    background-color: transparent;
    background-repeat: repeat-x;
}

input[type=checkbox]:checked + label{
    background-color: #3e618b;
    outline: 0;
}
<table class="example1" id="example" style="width:40%;" align='center'>
    <tr>
        <td>
            <input type="checkbox" id="chk1" name="chk" value="1">
              <label for="chk1">&nbsp; 1</label>
        </td>
        <td>
            <input type="checkbox" id="chk2" name="chk"value="2">
            <label for="chk2">&nbsp; 2</label>
        </td>
    </tr>
</table> 

<button>Changes checked checkbox color</button>

【问题讨论】:

  • 显示复选框的 html。
  • 在单击按钮时,您可以使用 jquery 添加到所有选定的复选框中,您可以使用 css 自定义(设置您想要的颜色)另一个类...如果您想要一个可行的解决方案,但是您应该提供您的 html。
  • 欢迎来到 StackOverflow.com。请在您的问题中添加一些代码。您可以使用jsfiddle.net 重现您的代码问题。
  • 请添加一些代码
  • 并非所有浏览器甚至都支持复选框样式或支持相同程度的样式。

标签: javascript jquery html css checkbox


【解决方案1】:

您可以向标签添加一个类,然后根据该类定义颜色,如下所示:

function toggle() {
  var labels = document.getElementsByTagName('label');
  for(var i = 0; i < labels.length; i++) {
    labels[i].classList.toggle('color');
  }
}
table.example1,
table.example1 th,
table.example1 td {
  border: 3px solid grey;
  max-width: 200px;
}

input[type=checkbox] {
  display: none;
}

input[type=checkbox]+label {
  display: inline-block;
  padding: 6px 60px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  color: white;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  background-color: transparent;
  background-repeat: repeat-x;
}

input[type=checkbox]:checked+label {
  background-color: #3e618b;
  outline: 0;
}
input[type=checkbox]:checked+label.color {
  background-color: red;
  outline: 0;
}
<table class="example1" id="example" style="width:40%;" align='center'>
  <tr>
    <td>
      <input type="checkbox" id="chk1" name="chk" value="1">
      <label for="chk1">&nbsp; 1</label>
    </td>
    <td>
      <input type="checkbox" id="chk2" name="chk" value="2">
      <label for="chk2">&nbsp; 2</label>
    </td>
  </tr>
</table>

<button onclick="toggle()">Changes radio button onclick</button>

【讨论】:

    【解决方案2】:

    您可以通过单击按钮更改父类的类属性。

    $("button").click(function() {
      $("table").toggleClass("alternative");
    });
    table.example1, table.example1 th, table.example1 td {
        border: 3px solid grey;
        max-width: 200px;
    }
    
    input[type=checkbox] {
        display:none;
    }
    
    input[type=checkbox] + label {
        display:inline-block;
        padding: 6px 60px;
        margin-bottom: 0;
        font-size: 14px;
        line-height: 20px;
        color: white;
        text-align: center;
        vertical-align: middle;
        cursor: pointer;
        background-color: transparent;
        background-repeat: repeat-x;
    }
    
    input[type=checkbox]:checked + label{
        background-color: #3e618b;
        outline: 0;
    }
    .alternative input[type=checkbox]:checked + label{
        background-color: #f00;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table class="example1" id="example" style="width:40%;" align='center'>
        <tr>
            <td>
                <input type="checkbox" id="chk1" name="chk" value="1">
                  <label for="chk1">&nbsp; 1</label>
            </td>
            <td>
                <input type="checkbox" id="chk2" name="chk"value="2">
                <label for="chk2">&nbsp; 2</label>
            </td>
        </tr>
    </table> 
    
    <button>Changes checked checkbox color</button>

    【讨论】:

    • 我只是将 descendant selector 添加到您的 CSS 和按钮单击事件处理程序中,以切换表格的特殊类。
    【解决方案3】:

    你可以用 CSS 来做到这一点

    隐藏原始输入字段

    input[type=checkbox]{
    visibility: hidden;
    position: absolute;
    }
    

    然后添加你自己的自定义 css 来显示字段

    input[type=checkbox] + label:before{
      height:18px;
      margin-right: 5px;
      content: " ";
      display:inline-block;
      vertical-align: baseline;
      border:1px solid #ccc;
      border-radius:4px;
      width:18px;
    }
    

    然后在选中复选框时添加所需样式和背景的 CSS

    input[type=checkbox]:checked + label:before{
          background: gold;
    }
    

    【讨论】:

    • 你用radio,可以和checkbox一起用吗?
    • 是的,只需更改复选框的单选:也许您可能希望将边框半径从 50% 更改为 4px 之类的东西
    • 我现在已经在我的回答中为您更改了它:您可以这样做,因为您所做的只是隐藏原始元素的可见性,然后您只需使用 CSS 显示其他内容,这可以是你想要的任何东西,甚至是一张图片作为背景等等......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-31
    • 2018-01-24
    • 2015-10-07
    • 1970-01-01
    • 2017-09-21
    • 2014-07-18
    相关资源
    最近更新 更多