Get <td> values only in specific rows using jQuery

别说谁变了你拦得住时间么 提交于 2019-12-05 14:49:06
<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>   

You can try something along this line:

$('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()
});  

Hope this works and helps!

$('#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() );

});
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!