复选框选择的限制 [英] Limit of checkbox selection
本文介绍了复选框选择的限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
美好的一天!我目前有此代码,但无法正常工作,即使我将其限制为最多只能选择2个项目,我仍然可以选择2个以上的代码.这是代码:
Good day! I currently have this code but it is not working and I can still select more than 2 even if I restrict it to maximum selection of 2 items. Here is the code:
$(document).ready(function() {
var limit = 2;
$('input.single-checkbox').on('change', function(evt) {
if ($(this).siblings(':checked').length >= limit) {
this.checked = false;
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
<label>Available Books</label>
<div>
<label>
<?php foreach($collections as $collection):?>
<input type="checkbox" id="blankCheckbox" class="single-checkbox" <?php echo ($collection->book_status=='borrowed'? 'disabled':'')?> name='accession_no[]' value='<?php echo $collection->id ?>' aria-label="...">
<?php echo $collection->title ?> - <?php echo $collection->author ?><br />
<?php endforeach;?>
</label>
</div>
</div>
推荐答案
引用此链接小提琴您可以找到一种方法来限制所选复选框的数量(如果您要搜索的是
referring to this link Fiddle you can find a way to limit the number of selected checkboxes if that is what you are searching for
var limit = 2;
$('input.single-checkbox').on('change', function(evt) {
if($(this).siblings(':checked').length >= limit) {
this.checked = false;
}
});
这篇关于复选框选择的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文