如何在动态创建的下一个td文本框中获得选中的具有相同值的复选框的计数

How to get count of checkbox checked which has same value in next td textbox dynamically created td?

本文关键字:复选框 创建 动态 下一个 td 文本      更新时间:2023-09-26

在这里,我可以同时选择多个复选框,并且复选框下一个td值可能重复。如果选中复选框,则下一个td值有两次相同的值,需要计数为2,依此类推。

我的桌子

echo '<tr style = "text-align:left"><td><input type = "checkbox" class = "selected_id_v" name = "selectd_prcs[]" value = '.$value->labdetail.'></td><td>'.$value->id.'</td></tr>

我试过什么

$(document).on('change','.selected_id_v',function () {
    if($(this).is(':checked'))
    {
    var parseInt(checked_val) = $(this).parent().parent().children().eq(1).text();
    }
});

您可以使用属性等于选择器

$(document).on('change', '.selected_id_v', function() {
  if (this.checked) {
    var checked_val = $(this).val();
    var $els = $('.selected_id_v[value="' + checked_val + '"]:checked');
    var count = $els.length;
    console.log(checked_val + ':' + count)
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr style="text-align:left">
    <td>
      <input type="checkbox" class="selected_id_v" name="selectd_prcs[]" value='1'>
    </td>
    <td>
      <input type="checkbox" class="selected_id_v" name="selectd_prcs[]" value='2'>
    </td>
    <td>
      <input type="checkbox" class="selected_id_v" name="selectd_prcs[]" value='3'>
    </td>
    <td>
      <input type="checkbox" class="selected_id_v" name="selectd_prcs[]" value='3'>
    </td>
    <td>
      <input type="checkbox" class="selected_id_v" name="selectd_prcs[]" value='2'>
    </td>
    <td>
      <input type="checkbox" class="selected_id_v" name="selectd_prcs[]" value='2'>
    </td>
  </tr>
</table>