我想知道通过互联网发现的这种带有图像的复选框。 (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解决,但是我有点困惑。
谢谢!
答案 0 :(得分:2)
只需一些简单的逻辑即可检查当前有多少个类,以及当前单击的元素是否具有该类,如果没有,则应将其禁用:
if ($('.image-checkbox-checked').length === 3 && !$(this).hasClass('image-checkbox-checked'))
return;
如果将其添加到onclick事件中,它应采取相应的措施。
这是一个分叉的代码笔: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>
答案 1 :(得分: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
}
});
答案 2 :(得分: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>