這邊用 bootstrap 建立一下 checkbox 的列表:
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="" name="checkbox" value="option1">
<label class="form-check-label" for="">1</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="" name="checkbox" value="option2">
<label class="form-check-label" for="">2</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="" name="checkbox" value="option3">
<label class="form-check-label" for="">3</label>
</div>
接著在勾選的時候檢查被選取的 checkbox 數量就好:
$(".form-check-input").click(function(){ let checkedCount=$(".form-check-input:checked").length; console.log(checkedCount) })