【发布时间】:2017-07-20 14:03:11
【问题描述】:
美好的一天!我目前有此代码,但它不起作用,即使我将其限制为最多选择 2 个项目,我仍然可以选择 2 个以上。代码如下:
$(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>
【问题讨论】:
-
你的代码 sn-p 不起作用
-
You might want to consider perhaps rather than unchecking the a checked box if more than two are selected, when two are selected, disabling all the others by adding a disabled attr -
<input type="checkbox" disabled />and then when the计数下降到两个以下,删除它们。我认为这更像是一种优雅且用户友好的方式。
标签: javascript java php jquery html