您的位置:首页 > 其它

点击table中的一行选中checkbox,并改变该行的颜色

2016-08-05 17:05 399 查看
首先规定一个选中时的样式:

.bgRed{
background-color: #b2dba1;
}


table的代码如下:

<table>
<tr>
<th><input type="checkbox" id="checkAll"> 序号</th>
<th>所属机构</th>
<th>姓名</th>
<th>手机号码</th>
</tr>
<td ><input type="checkbox" name="_dataCheckBox">1</td>
<td>山东</td>
<td>张三</td>
<td>15689547865</td>
</tr>
</table>


首先实现全选按钮功能:

$("#checkAll").click(function () {
if ($('#checkAll').attr('checked')) {
$("[name='_dataCheckBox']").prop("checked", 'true');//全选
$("[name='_dataCheckBox']").each(function () {
$(this).parent().parent().toggleClass("bgRed");//添加选中样式
});
} else {
$("[name='_dataCheckBox']").removeAttr("checked");//取消全选
$("[name='_dataCheckBox']").each(function () {
$(this).parent().parent().toggleClass("bgRed");//取消选中样式
});
}
});


实现点击一行选中checkbox,并改变这一行的样式:

//除了表头(第一行)以外所有的行添加click事件.
$("tr").first().nextAll().click(function () {
//如果没有某个样式则加上,否则去除
$(this).children().toggleClass("bgRed");
if ($(this).children().hasClass("bgRed")){//如果有某个样式则表明,这一行已经被选中
$(this).children().first().children().attr("checked", true);
} else {                                  //如果没有被选中
$(this).children().first().children().attr("checked", false);
}
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐