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

获取<;td>;使用jQuery仅在特定行中使用值,jquery,loops,rows,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.<

我有一个表(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>

文件标题
文件说明。
我需要遍历该表,确定用户选中了哪些复选框,对于带有复选框的行,获取第一个复选框的值和下两个复选框的文本

我不需要构建一个集合:在每个迭代中,我希望在其他地方更改一些元素。(对我来说)这不是困难的部分。它试图找出如何遍历该表并仅选择带有复选框的s。


<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>   
$(函数(){ $('#btnTest')。单击(函数(){ $('#docsTable input[type=“checkbox”]:checked')。每个(函数(){ var$row=$(this.parents('tr'); 警报($row.find('td:eq(0)input').val()); 警报($row.find('td:eq(1)').html()); 警报($row.find('td:eq(2)').html()); }); }); }); 文件标题1 文件说明。 文件标题2 文件说明。 文件标题3 文件说明。
您可以尝试以下方法:

$('tr > td:first-child > input:checked').each(function() {  
    // $(this).val() is the value of the input
    // $(this).parent().siblings() will refer to the two <td>'s  
    // You can also try other traversal methods like $(this).parent().next()
});  
$('tr>td:first child>input:checked')。每个(函数(){
//$(this).val()是输入的值
//$(this).parent().sides()将引用这两个
//您还可以尝试其他遍历方法,如$(this).parent().next()
});  

希望这有用

哇!在不到5分钟的时间内完成一个工作示例!这正是我要找的!我一直是Stackoverflow的搜索者,但这是我第一次在这里发布问题。你们太棒了@黄昏-很高兴我们能帮忙!最好的回馈方式是浏览网站并回答一些问题。你不仅会为社区做出贡献,而且你可以将你获得的声誉和个人资料作为简历的一部分。无论如何欢迎来到这个网站!
$('#docsTable > tr > td > input:checked').each(function(i,element){

var el = $(element);
alert( el.val() );
alert( el.parent().siblings(':eq(0)').html() );
alert( el.parent().siblings(':eq(1)').html() );

});