如何使多个复选框上的进度条工作?

时间:2018-05-23 08:16:41

标签: jquery html checkbox

我已勾选复选框上的进度条增量。它适用于单组复选框,但有多组复选框,它不能正常工作。

我想要什么

我想如果我使用多张卡,并且每张卡中都会有多个复选框,它们都应该单独使用



$('.card').each(function(index, el) {
  // console.log( $(this).find('input[type="checkbox"]').length );
  var chkLength = $(this).find('input[type="checkbox"]').length;
  var max = 100;
  var div = max / chkLength;

  $('.scrumboard .card .task-quantity').text('0/' + chkLength);

  // $(this).find('input[type="checkbox"]').append("<div>" + div +" </div>");
  $(this).find('input[type="checkbox"]').attr('value', div);
});

// Progress bar code
val = 0;
$('.card .new-control-input').on('change', function(event) {
  if ($(this).is(':checked')) {
    console.log('chk');
    // perc += parseInt($(this).val());
    // $(this).attr('checked', '');
    console.log(val += parseInt($(this).val()));
  } else {
    console.log('nchk');
    console.log(val -= parseInt($(this).val()));
  }

  $(".progress .progress-bar").each(function(index, el) {
    $(this).css("width", val + "%");
  });
})
&#13;
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="card">
  <input type="checkbox" class="new-control-input">
  <input type="checkbox" class="new-control-input">
  <input type="checkbox" class="new-control-input">
  <input type="checkbox" class="new-control-input">
  <input type="checkbox" class="new-control-input">
  <div class="progress">
    <div class="progress-bar bg-primary" role="progressbar" style="" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100"></div>
  </div>
  <p class="task-quantity"></p>
</div>
<div class="card">
  <input type="checkbox" class="new-control-input">
  <input type="checkbox" class="new-control-input">
  <input type="checkbox" class="new-control-input">
  <input type="checkbox" class="new-control-input">
  <input type="checkbox" class="new-control-input">
  <div class="progress">
    <div class="progress-bar bgbbg-primary" role="progressbar" style="" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100"></div>
  </div>
  <p class="task-quantity"></p>
</div>
&#13;
&#13;
&#13;

链接

1 个答案:

答案 0 :(得分:2)

问题是因为当复选框.progress .progress-bar事件触发时,您正在更新所有 DOM中的change元素。您需要使用DOM遍历来查找与引发事件的复选框相关的进度条。为此,您可以使用closest()获取父.card,然后find()

您还需要单独维护每个width,而不是共享val变量。为此,您可以使用直接存储在进度条上的data属性。试试这个:

$('.card').each(function(index, el) {
  var chkLength = $(this).find('input[type="checkbox"]').length;
  var max = 100;
  var div = max / chkLength;
  $('.scrumboard .card .task-quantity').text('0/' + chkLength);
  $(this).find('input[type="checkbox"]').attr('value', div);
});

$('.card .new-control-input').on('change', function(event) {
  var $checkbox = $(this);

  $checkbox.closest('.card').find(".progress .progress-bar").css("width", function() {
    var width = $(this).data('width') || 0;
    if ($checkbox.is(':checked')) {
      width += parseInt($checkbox.val());
    } else {
      width -= parseInt($checkbox.val());
    }
    $(this).data('width', width);
    return width + "%";
  });
})
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="card">
  <input type="checkbox" class="new-control-input">
  <input type="checkbox" class="new-control-input">
  <input type="checkbox" class="new-control-input">
  <input type="checkbox" class="new-control-input">
  <input type="checkbox" class="new-control-input">
  <div class="progress">
    <div class="progress-bar bg-primary" role="progressbar" style="" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100"></div>
  </div>
  <p class="task-quantity"></p>
</div>

<div class="card">
  <input type="checkbox" class="new-control-input">
  <input type="checkbox" class="new-control-input">
  <input type="checkbox" class="new-control-input">
  <input type="checkbox" class="new-control-input">
  <input type="checkbox" class="new-control-input">
  <div class="progress">
    <div class="progress-bar bgbbg-primary" role="progressbar" style="" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100"></div>
  </div>
  <p class="task-quantity"></p>
</div>