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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42668804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:18:50  来源:igfitidea点击:

Check if checkbox is checked inside table td

jqueryhtmlcheckboxhtml-table

提问by GeoDim

I am trying to build a script which checks if a checkbox is checked using jQuery $eachfunction.Below is the HTML code:

我正在尝试构建一个脚本,该脚本检查是否使用 jQuery$each函数检查了复选框。以下是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>

I believe it is something like (inside .each()):

我相信它类似于(在 .each() 中):

if ($("find_specific_checkbox").is(":checked")){
   console.log("Checked!");
}

Any ideas how I can do this. I am trying a couple of hours but nothing yet.

任何想法我怎么能做到这一点。我正在尝试几个小时,但还没有。

回答by Terry

Based on your question and markup, I believe that you intend to iterate through each table row element, and get the status of the checkbox in each row.

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

If that is the case, I have created both (1) a code sample and (2) a proof-of-concept example below that does what you want to do.

如果是这种情况,我已经创建了 (1) 代码示例和 (2) 下面的概念验证示例,可以执行您想要执行的操作。

Code sample

代码示例

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

Working example

工作示例

I have set the first checkbox to a checkedstatus, so you can see how the logic works.

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

$(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();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td class="categories">World</td>
    <td class="category_enabled" style="float: right;">
      <input type="checkbox" checked />
    </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>
</table>

回答by Sankar

You have to loop through every checkbox in that column and then check whether it is checked or not.

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

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

});
<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>