检查表td中是否选中了复选框

Geo*_*Dim 1 html checkbox jquery html-table

我正在尝试构建一个脚本,检查是否使用jQuery $each函数检查复选框.Below 是HTML代码:

 <tr>
  <td class="categories">World</td>
  <td class="category_enabled" style="float: right;">
  <input type="checkbox"/>
  </td>
 </tr>

 <tr>
  <td class="categories">Economy</td>
  <td class="category_enabled" style="float: right;">
  <input type="checkbox"/>
  </td>                            
 </tr>

 <tr>
   <td class="categories">?????????</td>
   <td class="category_enabled" style="float: right;">
   <input type="checkbox"/>
   </td>
 </tr>
Run Code Online (Sandbox Code Playgroud)

我相信它就像(内部.each()):

if ($("find_specific_checkbox").is(":checked")){
   console.log("Checked!");
}
Run Code Online (Sandbox Code Playgroud)

任何想法我怎么能这样做.我正在尝试几个小时,但还没有.

Ter*_*rry 5

基于您的问题和标记,我相信您打算遍历每个表行元素,并获取每行中复选框的状态。

在这种情况下,我创建了下面的两个示例:(1)一个代码示例和(2)一个概念验证示例,它可以完成您想要的工作。

代码样例

$('table tr').each(function(i) {
    // Cache checkbox selector
    var $chkbox = $(this).find('input[type="checkbox"]');

    // Only check rows that contain a checkbox
    if($chkbox.length) {
    var status = $chkbox.prop('checked');
        console.log('Table row '+i+' contains a checkbox with a checked status of: '+status);
    }
});
Run Code Online (Sandbox Code Playgroud)

工作实例

我已经将第一个复选框设置为checked状态,因此您可以看到逻辑的工作方式。

$('table tr').each(function(i) {
    // Cache checkbox selector
    var $chkbox = $(this).find('input[type="checkbox"]');

    // Only check rows that contain a checkbox
    if($chkbox.length) {
    var status = $chkbox.prop('checked');
        console.log('Table row '+i+' contains a checkbox with a checked status of: '+status);
    }
});
Run Code Online (Sandbox Code Playgroud)
$(function() {
  // General/modular function for status logging
  var checkboxChecker = function() {
    $('table tr').each(function(i) {
      // Only check rows that contain a checkbox
      var $chkbox = $(this).find('input[type="checkbox"]');
      if ($chkbox.length) {
        var status = $chkbox.prop('checked');
        console.log('Table row ' + i + ' contains a checkbox with a checked status of: ' + status);
      }
    });
  };
  
  // Check checkboxes status on DOMready
  checkboxChecker();
  
  // Check again when checkboxes states are changed
  $('table tr input[type="checkbox"]').on('change', function() {
    checkboxChecker();
  });
});
Run Code Online (Sandbox Code Playgroud)


San*_*kar 5

您必须遍历该列中的每个复选框,然后检查是否已选中它.

$(document).ready(function() {
  $('table [type="checkbox"]').each(function(i, chk) {
    if (chk.checked) {
      console.log("Checked!", i, chk);
    }
  });

});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<table>
  <tr>
    <td class="categories">World</td>
    <td class="category_enabled" style="float: right;">
      <input type="checkbox" />
    </td>
  </tr>

  <tr>
    <td class="categories">Economy</td>
    <td class="category_enabled" style="float: right;">
      <input type="checkbox" checked />
    </td>
  </tr>

  <tr>
    <td class="categories">?????????</td>
    <td class="category_enabled" style="float: right;">
      <input type="checkbox" />
    </td>
  </tr>
</table>
Run Code Online (Sandbox Code Playgroud)