【问题标题】:How do I get the checkbox value from a specific cell in a table row using jQuery?如何使用 jQuery 从表格行中的特定单元格获取复选框值?
【发布时间】:2011-03-19 06:41:34
【问题描述】:

我有一个表格,每行包含多个复选框。我需要获取每个复选框的值。我已经能够执行以下操作来检索表第一列中复选框的值...

$('#myTable tbody tr td:first-child input:checkbox').each(function() {
        if (this.checked) {
            //Do something
        }
    });

但现在我需要遍历每一行,并获取每个复选框的值。这是我一直在尝试的特定复选框...

if($(this).find(':input[type="checkbox"]').eq(0).attr('checked')) {
            myVariable = 'so and so';
        }
        else {
            myVariable = 'something else';
        }

这不起作用。关于如何让这个工作的任何建议?

【问题讨论】:

    标签: jquery checkbox tablecell


    【解决方案1】:

    您的问题不是那么清楚,但是从您的第二个示例开始,我假设“this”是 tr。下面显示了如何获取行内的第一个复选框并测试它是否被选中

    if ( $(this).find('input:checkbox:first').is(':checked') ) {...}
    

    迭代行中的所有复选框

    $(this).find('input:checkbox').each( function(){...}
    

    迭代行中所有选中的复选框

       $(this).find('input:checkbox:checked').each( function(){...}
    

    【讨论】:

      【解决方案2】:

      为什么不从您的选择器中删除td:first-child

      $('#myTable tbody tr  input:checkbox').each(function() {
          if (this.checked) {
              //Do something
          }
      });
      

      或者,如果您想以某种方式对每行的复选框进行分组:

      $('#myTable tbody tr').each(function() {
          $(this).find('input:checkbox:checked').each(function() {
              //..
          });
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-05
        • 1970-01-01
        • 2014-10-19
        • 1970-01-01
        • 1970-01-01
        • 2010-09-27
        • 1970-01-01
        • 2013-09-18
        相关资源
        最近更新 更多