jquery高亮表行如果复选框被选中 [英] jquery highlight table row if checkbox is checked

查看:145
本文介绍了jquery高亮表行如果复选框被选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何突出显示表格行,如果我点击一个元素。

I know how to highlight a table row, if I "click" an element.

但是当我打开一个页面,一些复选框已经选中。我想在页面加载时使用jquery突出显示这些行。

But when I open a page, some checkboxes are already checked. I want to highlight those rows, using jquery, when the page loads.

我给所有的复选框一个复选框类。
这里是我到目前为止:

I gave all my checkboxes a class of "checkboxes". Here's what I got so far:

$(document).ready(function(){ 

   if( $('.checkboxes').attr("checked") == true ){ /*not sure how to detect a row*/  }

});

我不确定要放在里面。

我得到的最接近的是放置这个:

The closest I got was to place this:

$(this).closest('tr').addClass("pinkrow");

但是 $(this)

推荐答案

您可以使用 .each() [docs] http://api.jquery.com/checked-selector/ =nofollow> :checked [docs] 伪选择器:

You can use .each() [docs] and the :checked [docs] pseudo selector:

$('tr .checkboxes:checked').each(function() {
    $(this).closest('tr').addClass('pinkrow');
});

如果只想使用纯CSS选择器,可以测试一个元素是否被DOM元素检查属性:

If you want to use pure CSS selectors only, you can test whether an element is checked with the DOM element's checked property:

$('tr .checkboxes').each(function() {
    if(this.checked) {
        $(this).closest('tr').addClass('pinkrow');
    }
});

另请注意, tr .checkboxes 那些 .checkboxes 元素,它们在表行中(如果有其他)。

Also note that tr .checkboxes only selects those .checkboxes elements, which are in a table row (in case there are others).

这篇关于jquery高亮表行如果复选框被选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆