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

时间:2016-07-27 11:39:17

标签: javascript jquery checkbox

在这里我可以同时选择多个复选框。复选框下一个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();

    }
});

1 个答案:

答案 0 :(得分:0)

您可以使用属性equals selector

&#13;
&#13;
$(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)
  }
});
&#13;
<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>
&#13;
&#13;
&#13;

相关问题