当选中某个checkbox,该元素所在的行背景色加深。取消选中时,所在行背景色变白。全选和取消全选都可以了,就是部分选择然后反选时有点问题,估计是本人JQUERY水平差所致。直接看代码吧
$("#fanxuan").click(function(){
$("input[type=checkbox]").each(function(){
if(this.checked){
this.checked = false;
$(this).parents("tr").style.backgroundColor="#ffffff";
}else{
this.checked = true;
$(this).parents("tr").style.backgroundColor="#D3E6F8";
}
});
});
大概意思是遍历所有checkbox,然后取反。到这里功能正常,但是$(this).parents("tr").style.backgroundColor="#ffffff";和$(this).parents("tr").style.backgroundColor="#D3E6F8";就出现问题了,背景色却不会跟着变化。请问,是哪里写的不对呢?
$("#fanxuan").click(function(){
$("input[type=checkbox]").each(function(){
if(this.checked){
this.checked = false;
$(this).parents("tr").style.backgroundColor="#ffffff";
}else{
this.checked = true;
$(this).parents("tr").style.backgroundColor="#D3E6F8";
}
});
});
大概意思是遍历所有checkbox,然后取反。到这里功能正常,但是$(this).parents("tr").style.backgroundColor="#ffffff";和$(this).parents("tr").style.backgroundColor="#D3E6F8";就出现问题了,背景色却不会跟着变化。请问,是哪里写的不对呢?
$("#fanxuan").bind('click',function(){
$("input[type=checkbox]").each(function(){
if(this.checked){
this.checked = false;
$(this).parents("td")[0].parentNode.style.backgroundColor="#ffffff";
}
else{
this.checked = true;
$(this).parents("td")[0].parentNode.style.backgroundColor="#D3E6F8";
}
});
})
});
<s:iterator value="pageDataList" status="status">
<tr onmouseover="$(this).addClass('mouse_over');" onmouseout="$(this).removeClass('mouse_over');">
<td nowrap><input type="checkbox" id="${ID}" value="${ID}" name="checkname"/>${status.count}</td>
<td>${QYMC}</td>
<td>${QYDQCODE}</td>
<td nowrap>${QYSSHY}</td>
<td nowrap>${UPDATETIME}</td>
<td nowrap>${UPDATEUSER}</td>
<app:ckact item="xxxx">
<td nowrap>
<a href="${ctx}/qy/qypreview?id=${ID}" target="_cg">预览</a>
<a href="${ctx}/qy/showqy?id=${ID}">修改</a>
</td>
</app:ckact>
</tr>
</s:iterator>
</tbody>
<table cellspacing="0" border="1">
<tr>
<td>
<input type="checkbox" />checkbox</td>
<td>Some text here</td>
<td>Extra text here</td>
</tr>
<tr>
<td>
<input type="checkbox" />checkbox</td>
<td>Some text here</td>
<td>Extra text here</td>
</tr>
</table>[/code
Javascript代码如下:
[code=JScript]$('table :checkbox').click(function () {
$(this).parent().parent().css('background',$(this).attr('checked')? 'red':'blue');
});
如果其他行还有checkbox,那么使用更强力的选择器就好
如果此checkbox在其他列出现、或者此checkbox嵌套了多层Dom,那么你的选择器就需要更加准确,仅此而已。