简体   繁体   English

使用 html 中的复选框在表格上切换颜色

[英]Toggling colour on a table using check boxes in html

So I have a table like this所以我有一张这样的桌子

<table class="rest_tab" border="1">
<tbody><tr><th>Times</th><th></th><th></th></tr>
<tr><td <="" td=""></td><td align="right"><input type="checkbox" class="selectAll" id="Checkbox4" on=""></td><td align="right"><input  type="checkbox" class="selectAll" id="Checkbox5" on=""></td></tr>
<tr><td <="" td=""></td><td align="right"><input type="checkbox" class="selectAll" id="Checkbox7" on=""></td><td align="right"><input type="checkbox" class="selectAll" id="Checkbox8" on=""></td></tr>
<tr><td <="" td=""></td><td align="right"><input type="checkbox" class="selectAll" id="Checkbox10" on=""></td><td align="right"><input type="checkbox" class="selectAll" id="Checkbox11" on=""></td></tr>
</tbody></table>

Demo:演示:

 $(document).ready(function() { $('.pretty tr').toggle(function() { $(this).find(':checkbox').attr('checked', true); $(this).toggleClass('red'); }, function() { $(this).toggleClass('red'); $(this).find(':checkbox').attr('checked', false); }); });
 table { font-family: 'Arial'; margin: 25px auto; border-collapse: collapse; border: 1px solid #eee; border-bottom: 2px solid #00cccc; box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.1), 0px 10px 20px rgba(0, 0, 0, 0.05), 0px 20px 20px rgba(0, 0, 0, 0.05), 0px 30px 20px rgba(0, 0, 0, 0.05); } table td:hover { background: #C1FFC1; } table td:click { background: #fcc; } table td:hover { color: #000; } table th, table td { color: #999; border: 1px solid #eee; padding: 12px 35px; border-collapse: collapse; } table th { background: #00cccc; color: #fff; text-transform: uppercase; font-size: 12px; } table th.last { border-right: none; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table class="rest_tab" border="1"> <tbody> <tr> <th>Times</th> <th></th> <th></th> </tr> <tr> <td <="" td=""></td> <td align="right"><input type="checkbox" class="selectAll" id="Checkbox4" on=""></td> <td align="right"><input type="checkbox" class="selectAll" id="Checkbox5" on=""></td> </tr> <tr> <td <="" td=""></td> <td align="right"><input type="checkbox" class="selectAll" id="Checkbox7" on=""></td> <td align="right"><input type="checkbox" class="selectAll" id="Checkbox8" on=""></td> </tr> <tr> <td <="" td=""></td> <td align="right"><input type="checkbox" class="selectAll" id="Checkbox10" on=""></td> <td align="right"><input type="checkbox" class="selectAll" id="Checkbox11" on=""></td> </tr> </tbody> </table>

I want to be able to, instead of just changing colour while hovering retain the colour while check box is checked for each box in the table.我希望能够,而不仅仅是在悬停时更改颜色,而在为表格中的每个框选中复选框时保留颜色。

I've tried for quite a while now and cannot get the js to do what I want, any help would be greatly appreciated.我已经尝试了很长一段时间,但无法让 js 做我想做的事,任何帮助将不胜感激。

Have a css rule like this which will help you to toggle checkbox selection有一个像这样的 css 规则可以帮助你切换复选框选择

.selected{
    background-color: #C1FFC1;
}

In your js you can do something like this在你的 js 你可以做这样的事情

$('.selectAll').change(function(){
  $(this).closest('td').toggleClass('selected');
});

Updated your jsfiddle demo更新了你的jsfiddle 演示

 $(document).ready(function () { $('.selectAll').change(function(){ $(this).closest('td').toggleClass('selected'); }); });
 table { font-family:'Arial'; margin: 25px auto; border-collapse: collapse; border: 1px solid #eee; border-bottom: 2px solid #00cccc; box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.1), 0px 10px 20px rgba(0, 0, 0, 0.05), 0px 20px 20px rgba(0, 0, 0, 0.05), 0px 30px 20px rgba(0, 0, 0, 0.05); } table td:hover { background: #C1FFC1; } table td:click { background: #fcc; } table td:hover { color: #000; } table th, table td { color: #999; border: 1px solid #eee; padding: 12px 35px; border-collapse: collapse; } table th { background: #00cccc; color: #fff; text-transform: uppercase; font-size: 12px; } table th.last { border-right: none; } .selected{ background-color: #C1FFC1; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="rest_tab" border="1"> <tbody> <tr> <th>Times</th> <th></th> <th></th> </tr> <tr> <td <="" td=""></td> <td align="right"> <input type="checkbox" class="selectAll" id="Checkbox4" on=""> </td> <td align="right"> <input type="checkbox" class="selectAll" id="Checkbox5" on=""> </td> </tr> <tr> <td <="" td=""></td> <td align="right"> <input type="checkbox" class="selectAll" id="Checkbox7" on=""> </td> <td align="right"> <input type="checkbox" class="selectAll" id="Checkbox8" on=""> </td> </tr> <tr> <td <="" td=""></td> <td align="right"> <input type="checkbox" class="selectAll" id="Checkbox10" on=""> </td> <td align="right"> <input type="checkbox" class="selectAll" id="Checkbox11" on=""> </td> </tr> </tbody> </table>

Hope this helps希望这可以帮助

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

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