javascript - 我想修改下面的代码以选择最多 3 个复选框
问题描述
我创建了多个复选框,但我想选择最多 3 个复选框。早些时候我使用下面的代码只选择一个复选框,当我编辑复选框时,它会返回之前选择的复选框。我希望它按原样发生,但一个更正是....它应该选择最多 3 个复选框。谁能按照我的要求给我一个代码。
1.code
<div class = "form-group">
<label> Employee Skills </label> <br>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<input class="selectupto3" type="checkbox" th:field = "*{skill}" name = "skill" value="Java" />Java <br>
<input class="selectupto3" type="checkbox" th:field = "*{skill}" name = "skill" value="Python" />Python <br>
<input class="selectupto3" type="checkbox" th:field = "*{skill}" name = "skill" value=".Net" />.Net <br>
<input class="selectupto3" type="checkbox" th:field = "*{skill}" name = "skill" value="Kotlin" />Kotlin <br>
<input class="selectupto3" type="checkbox" th:field = "*{skill}" name = "skill" value="C" />C <br>
<input class="selectupto3" type="checkbox" th:field = "*{skill}" name = "skill" value="Ruby" />Ruby <br>
<script type="text/javascript">
$('.selectupto3').on('change', function() {
$('.selectupto3').not(this).prop('checked', false);
});
</script>
<span th:if="${#fields.hasErrors('skill')}" th:errors="*{skill}">skill error</span>
</div>
2.I want to modify this below code for selecting up to 3 checkboxes and also when i update these checkboxes it should return with previous selected checkboxes.
<script type="text/javascript">
$('.selectupto3').on('change', function() {
$('.selectupto3').not(this).prop('checked', false);
});
</script>
解决方案
推荐阅读
- sql-server - SQL Server:Charindex 与标准 where 子句语句,哪个更有效?
- excel - 在 DAX 中,我可以创建一个计算两个日期/时间字段之间的工作日的函数吗?
- java - 为什么使用规则引擎而不是易于理解的一行属性?
- mysql - MySQL中的成本常数
- c - C语言猜数字游戏
- python - 使用 Flask 从 S3 存储桶下载文件
- r - 确定函数调用中是否已给出值的方法
- html - CSS - 边框不隐藏在 Safari 中
- python - 在 python 中安装和导入模块的问题
- ruby-on-rails - Rails Activerecord:如何排除具有多个关联记录的记录