【问题标题】:Get <td> values only in specific rows using jQuery使用 jQuery 仅在特定行中获取 <td> 值
【发布时间】:2011-09-12 17:27:17
【问题描述】:

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

【问题讨论】:

    标签: jquery loops rows


    【解决方案1】:
    <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>   
    

    【讨论】:

    • 哇!不到 5 分钟的工作示例!正是我想要的!我一直是 Stackoverflow 的长期搜索者,但这是我第一次在这里发布问题。你们摇滚!
    • @eventide - 很高兴我们能够提供帮助!回馈的最佳方式是浏览网站并回答一些问题。您不仅会为社区做出贡献,而且可以将获得的声誉和个人资料作为简历的一部分。无论如何...欢迎来到这个网站!
    【解决方案2】:

    您可以尝试以下方法:

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

    希望这有效并有所帮助!

    【讨论】:

      【解决方案3】:
      $('#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() );
      
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-06-16
        • 1970-01-01
        • 1970-01-01
        • 2010-11-19
        • 2015-08-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多