使用jQuery仅在特定行中获取<td>值

mar*_*rky 4 jquery loops rows

我有一个表(id ="docsTable"),其行看起来类似于:

<tr bgcolor="#E7DDCC">
    <td align="center"><input type="checkbox" name="docsToAddToClass[]" value="35" /></td>
    <td>Document Title</td>
    <td>Document Description.</td>
</tr>
Run Code Online (Sandbox Code Playgroud)

我需要遍历表,确定用户已选中的复选框,对于带有复选复选框的行,请获取第一个的值和下两个的文本.

我不需要构建集合:在每次迭代中,我想在其他地方更改一些元素.这不是困难的部分(对我来说).它试图弄清楚如何遍历表并仅选中带有选中复选框的s.

Bra*_*one 8

<html>
  <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" ></script>
    <script type="text/javascript">
        $(function(){
            $('#btnTest').click(function(){
                $('#docsTable input[type="checkbox"]:checked').each(function(){
                    var $row = $(this).parents('tr'); 
                    alert($row.find('td:eq(0) input').val());
                    alert($row.find('td:eq(1)').html());
                    alert($row.find('td:eq(2)').html());
                });
            });
        });
    </script>
  </head>
  <body>
    <table id="docsTable">
        <tr bgcolor="#E7DDCC">
            <td align="center"><input type="checkbox" name="docsToAddToClass[]" value="35" /></td>
            <td>Document Title 1</td>
            <td>Document Description.</td>
        </tr>
        <tr bgcolor="#E7DDCC">
            <td align="center"><input type="checkbox" name="docsToAddToClass[]" value="36" /></td>
            <td>Document Title 2</td>
            <td>Document Description.</td>
        </tr>
        <tr bgcolor="#E7DDCC">
            <td align="center"><input type="checkbox" name="docsToAddToClass[]" value="37" /></td>
            <td>Document Title 3</td>
            <td>Document Description.</td>
        </tr>
    </table>
    <input type='button' id='btnTest' value='Get Rows' />
  </body>
</html>   
Run Code Online (Sandbox Code Playgroud)

  • @eventide - 很高兴我们能够提供帮助!回馈的最佳方式是浏览网站并回答一些问题.您不仅会为社区做出贡献,还可以将您获得的声誉和个人资料用作简历的一部分.无论如何......欢迎来到网站! (2认同)