[英]JQuery: Change the color of the row inside a table based on the checkbox click
I have a table
that has huge set of data. 我有一个包含大量数据的
table
。 Now what i want to do is when user will uncheck the checkbox of that specific row, i want to change the color to grey. 现在我想要做的是当用户取消选中该特定行的复选框时,我想将颜色更改为灰色。 What i have done till now:
到目前为止我做了什么:
$('tr').click(function () {
if(this.style.background == "" || this.style.background =="white") {
$(this).css('background', 'grey');
}
else {
$(this).css('background', 'white');
}
});
$(".uncheck").change(function(){
alert("cehc");
var ischecked= $(this).is(':checked');
alert(ischecked);
if(!ischecked){
$(this).css('background', 'grey');
}
else{
$(this).css('background', 'white');
}
});
The above functions i have written that work properly individually though. 我编写的上述功能虽然可以单独使用。 Its like The first function will work when i click on row and second function puts an alert on unchecking the checkbox but does not change the color to grey.
它就像当我点击行时第一个函数将起作用,第二个函数在取消选中复选框时发出警告但不会将颜色更改为灰色。
How to identify if a specific checkbox has been unchecked on a row of a table and change that row color to grey? 如何识别是否在表的一行上取消选中特定复选框并将该行颜色更改为灰色?
The this
in change
refers to the checkbox and not to the row. 在
this
中change
是指该复选框,而不是行。 You could target your row using the closest
method: 您可以使用
closest
方法定位您的行:
$(this).closest('tr').css('background', 'grey');
$('tr').click(function () { if(this.style.background == "" || this.style.background =="white") { $(this).css('background', 'grey'); } else { $(this).css('background', 'white'); } }); $(".uncheck").change(function(){ alert("check change"); var ischecked= $(this).is(':checked'); alert(ischecked); if(!ischecked){ $(this).closest('tr').css('background', 'grey'); } else{ $(this).closest('tr').css('background', 'white'); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tbody> <tr> <td><input type="checkbox" class="uncheck"/></td> <td>1</td> <td>1</td> <td>1</td> </tr> </tbody> </table>
Try using single function and use css classes for that: 尝试使用单个函数并使用css类:
$(document).ready(function() { $('tr').click(function() { var inp = $(this).find('.check'); var tr = $(this).closest('tr'); inp.prop('checked', !inp.is(':checked')) tr.toggleClass('isChecked', inp.is(':checked')); }); // do nothing when clicking on checkbox, but bubble up to tr $('.check').click(function(e) { e.preventDefault(); }) })
.isChecked { background-color: grey; } table { width: 100%; border-collapse: collapse; } tr { background-color: #fafafa; } /* click-through element */ .check { pointer-events: none; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td> <input type="checkbox" class="check"> </td> <td></td> </tr> <tr> <td> <input type="checkbox" class="check"> </td> <td></td> </tr> <tr> <td> <input type="checkbox" class="check"> </td> <td></td> </tr> <tr> <td> <input type="checkbox" class="check"> </td> <td></td> </tr> </table>
Try this: 尝试这个:
$(".uncheck").change(function(){
$(this).toggleClass('grey white')
});
This should be your css 这应该是你的CSS
.grey { background: grey; }
.white { background: white; }
you can check the checkbox property: 你可以查看复选框属性:
$(".uncheck").change(function(){
alert("cehc");
var ischecked= $(this).prop('checked');
alert(ischecked);
if(!ischecked){
$(this).parent().closest('tr').css('background', 'grey');
}
else{
$(this).parent().closest('tr').css('background', 'white');
}
});
If you have checkbox inside <table>
, then You can use below code: 如果您在
<table>
有复选框,那么您可以使用以下代码:
$(".uncheck").change(function(){
var ischecked= $(this).is(':checked');
if(!ischecked){
$(this).closest('tr').css('background', 'grey');
}
else{
$(this).closest('tr').css('background', 'white');
}
});
on()
any checkbox
that triggers the change
event do this: on()
任何触发change
事件的checkbox
都执行此操作:
Use
this
closest('tr')
and.find('td')
to.css('background','grey')
使用
this
closest('tr')
和.find('td')
到.css('background','grey')
$('input[type="checkbox"]').on('change', function(e) { var self = this; if (!self.checked) { $(this).closest('tr').find('td').css('background', 'grey'); } });
table, th, td { border: 1px solid black; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <thead> <tr> <th>CHX</th> <th>Col1</th> <th>Col2</th> </tr> </thead> <tbody> <tr> <td> <input type='checkbox' checked> </td> <td></td> <td></td> </tr> <tr> <td> <input type='checkbox' checked> </td> <td></td> <td></td> </tr> <tr> <td> <input type='checkbox' checked> </td> <td></td> <td></td> </tr> <tr> <td> <input type='checkbox' checked> </td> <td></td> <td></td> </tr> <tr> <td> <input type='checkbox' checked> </td> <td></td> <td></td> </tr> <tr> <td> <input type='checkbox' checked> </td> <td></td> <td></td> </tr> <tr> <td> <input type='checkbox' checked> </td> <td></td> <td></td> </tr> <tr> <td> <input type='checkbox' checked> </td> <td></td> <td></td> </tr> </tbody> <tfoot> <tr> <td colspan='3'> </td> </tr> </tfoot>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.