Jquery 使用复选框选择表行 [英] Jquery Selecting Table Rows with Checkbox

查看:23
本文介绍了Jquery 使用复选框选择表行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子

<table id="rowclick2">
  <tbody>
    <tr >
      <td class="cb"><input type="checkbox" value="yes"></td>
      <td>row 1</td>
       <td>A</td>
    </tr>    
    <tr >
      <td class="cb"><input type="checkbox" value="yes" ></td>
      <td>row 2</td>
       <td>B</td>
    </tr>    
    <tr>
      <td class="cb"><input type="checkbox" value="yes"></td>
      <td>row 3</td>
      <td>C</td>  
    </tr>    
  </tbody>
</table>

我想在选中复选框的行中获取每个单元格(单击按钮时)

where I want to get each cell (when the button is clicked) in a row whose checkboxes are checked

我试过过滤

  $('#test').click(function(){

      $('#rowclick2 tr').filter(':has(:checkbox:checked)').each(
          //get row values
      );
    });

很简单,但我看不到我错过了什么......

It's pretty simple yet I can't see what I am missing...

这里是jsfiddle链接...

Here is the jsfiddle link...

推荐答案

你就快到了 :)

如果您希望选中复选框的行中的所有 s:

If you want all <td> s in rows where the checkbox is checked:

$('#rowclick2 tr').filter(':has(:checkbox:checked)').find('td');

例如:

$('#rowclick2 tr').filter(':has(:checkbox:checked)').find('td').each(function() {
    // this = td element
});

更详细的示例在 JsFiddle 上.

More elaborate example is on JsFiddle.

顺便说一句
.filter(':has(:checkbox:checked)') 可以写成 .has(':checkbox:checked') 如果你像我一样发现更容易阅读.

BTW
The .filter(':has(:checkbox:checked)') can be written as .has(':checkbox:checked') if you, like me, find that easier to read.

这篇关于Jquery 使用复选框选择表行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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