如果有的选择如何禁用现有的复选框的休息

问题描述 投票:1回答:3

我是通过互联网知道,我发现这种类型的带有图像的复选框。 (https://codepen.io/kskhr/pen/pRwKjg)我需要在选择3禁用的复选框的其余部分。

JS

// image gallery
// init the state from the input
$(".image-checkbox").each(function () {
  if ($(this).find('input[type="checkbox"]').first().attr("checked")) {
    $(this).addClass('image-checkbox-checked');
  }
  else {
    $(this).removeClass('image-checkbox-checked');
  }
});

// sync the state to the input
$(".image-checkbox").on("click", function (e) {
  $(this).toggleClass('image-checkbox-checked');
  var $checkbox = $(this).find('input[type="checkbox"]');
  $checkbox.prop("checked",!$checkbox.prop("checked"))

  e.preventDefault();
});

我认为最好的办法可以用JavaScript来解决,但我是一个有点困惑。

谢谢!

javascript jquery css html5 css3
3个回答
2
投票

只是一些简单的逻辑来检查有多少目前拥有的类,并且如果当前点击的元素有类,如果没有它应被禁用:

if ($('.image-checkbox-checked').length === 3 && !$(this).hasClass('image-checkbox-checked')) 
    return;

如果您添加到onclick事件应该采取相应的行动。

这里是一个分叉codepen:https://codepen.io/anon/pen/Rvyqyq

Codepen不喜欢这里某种原因,是一个片段,如果它不加载:

// image gallery
// init the state from the input
$(".image-checkbox").each(function() {
  if ($(this).find('input[type="checkbox"]').first().attr("checked")) {
    $(this).addClass('image-checkbox-checked');
  } else {
    $(this).removeClass('image-checkbox-checked');
  }
});

// sync the state to the input
$(".image-checkbox").on("click", function(e) {

  if ($('.image-checkbox-checked').length === 3 && !$(this).hasClass('image-checkbox-checked')) return;

  $(this).toggleClass('image-checkbox-checked');
  var $checkbox = $(this).find('input[type="checkbox"]');
  $checkbox.prop("checked", !$checkbox.prop("checked"))

  e.preventDefault();
});
.nopad {
  padding-left: 0 !important;
  padding-right: 0 !important;
}


/*image gallery*/

.image-checkbox {
  cursor: pointer;
  box-sizing: border-box;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  border: 4px solid transparent;
  margin-bottom: 0;
  outline: 0;
}

.image-checkbox input[type="checkbox"] {
  display: none;
}

.image-checkbox-checked {
  border-color: #4783B0;
}

.image-checkbox .fa {
  position: absolute;
  color: #4A79A3;
  background-color: #fff;
  padding: 10px;
  top: 0;
  right: 0;
}

.image-checkbox-checked .fa {
  display: block !important;
}
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<!-- 
Image Checkbox Bootstrap template for multiple image selection
https://www.prepbootstrap.com/bootstrap-template/image-checkbox
-->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css">
<div class="container">
  <h3>Bootstrap image checkbox(multiple)</h3>
  <div class="col-xs-4 col-sm-3 col-md-2 nopad text-center">
    <label class="image-checkbox">
      <img class="img-responsive" src="https://dummyimage.com/600x400/000/fff" />
      <input type="checkbox" name="image[]" value="" />
      <i class="fa fa-check hidden"></i>
    </label>
  </div>
  <div class="col-xs-4 col-sm-3 col-md-2 nopad text-center">
    <label class="image-checkbox">
      <img class="img-responsive" src="https://dummyimage.com/600x400/000/fff" />
      <input type="checkbox" name="image[]" value="" />
      <i class="fa fa-check hidden"></i>
    </label>
  </div>
  <div class="col-xs-4 col-sm-3 col-md-2 nopad text-center">
    <label class="image-checkbox">
      <img class="img-responsive" src="https://dummyimage.com/600x400/000/fff" />
      <input type="checkbox" name="image[]" value="" />
      <i class="fa fa-check hidden"></i>
    </label>
  </div>
  <div class="col-xs-4 col-sm-3 col-md-2 nopad text-center">
    <label class="image-checkbox">
      <img class="img-responsive" src="https://dummyimage.com/600x400/000/fff" />
      <input type="checkbox" name="image[]" value="" />
      <i class="fa fa-check hidden"></i>
    </label>
  </div>
  <div class="col-xs-4 col-sm-3 col-md-2 nopad text-center">
    <label class="image-checkbox">
      <img class="img-responsive" src="https://dummyimage.com/600x400/000/fff" />
      <input type="checkbox" name="image[]" value="" />
      <i class="fa fa-check hidden"></i>
    </label>
  </div>

</div>

0
投票

创建一个计数器来跟踪多少复选框被选中:

//html
<input type="checkbox" class="checkbox">1
<input type="checkbox" class="checkbox">2
<input type="checkbox" class="checkbox">3                            

-

let numberOfCheckboxesChecked = 0;

$('.checkbox').on("change", (e) => {
  if (e.target.checked) {
     numberOfCheckboxesChecked +=1 ;
  } else {
      numberOfCheckboxesChecked -=1;
  }

  if (numberOfCheckboxesChecked > 3) {
    e.target.checked = false;
   // or some other code to disable the remaining inputs
  }
});

0
投票

let count = 0;
$(".checkbox").on("click", function() {
  if ($(this).is(":checked")) {
    $(this).removeClass("undone").addClass("done")
    count++;
  } else {
   $(this).removeClass("done").addClass("undone")
    count--
  }
  if (count == "3") {
    $(".checkbox").each(function() {
      if ($(".checkbox").hasClass("undone")) {
        $(".undone").attr("disabled", "disabled")
      }
    });
  }
  else{
   $(".checkbox").attr("disabled", false)
   
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <label>
  <input class="checkbox undone" type="checkbox" value="Option1">Option 1</label>
</div>
<div>
  <label>
  <input class="checkbox  undone" type="checkbox" value="Option2">Option 2</label>
</div>
<div>
  <label><input class="checkbox  undone" type="checkbox" value="Option3" >Option 3</label>
</div>
<div>
  <label>
  <input class="checkbox undone" type="checkbox" value="Option4">Option 4</label>
</div>
<div>
  <label>
  <input class="checkbox undone" type="checkbox" value="Option5">Option 5</label>
</div>
<div>
  <label>
  <input class="checkbox  undone" type="checkbox" value="Option6" >Option 6</label>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.