同一班级选中了多个复选框

时间:2019-03-18 11:40:55

标签: javascript jquery

我有多个与AJAX一起添加的复选框。如果未选中此复选框,我将尝试触发click,但这不适用于所有复选框。

if ($(".variable_manage_stock").prop('checked') == false) {
  $('.variable_manage_stock').trigger('click');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" class="variable_manage_stock" name="variable_manage_stock[0]">
<input type="checkbox" class="variable_manage_stock" name="variable_manage_stock[1]">
<input type="checkbox" class="variable_manage_stock" name="variable_manage_stock[2]">
<input type="checkbox" class="variable_manage_stock" name="variable_manage_stock[3]">

1 个答案:

答案 0 :(得分:2)

您可以像以下代码那样遍历$(".variable_manage_stock")所获得的列表:

var stockList = $(".variable_manage_stock");
stockList.each(function() {
  if ($(this).prop('checked') == false) {
    $(this).trigger('click');
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" class="variable_manage_stock" name="variable_manage_stock[0]">
<input type="checkbox" class="variable_manage_stock" name="variable_manage_stock[1]">
<input type="checkbox" class="variable_manage_stock" name="variable_manage_stock[2]">
<input type="checkbox" class="variable_manage_stock" name="variable_manage_stock[3]">