javascript - 在上一个下拉字段中选择的禁用/隐藏下拉选项(从数据库中获取)
问题描述
我有多个下拉菜单都有相同的选项。在特定的下拉字段中,我想禁用或隐藏在前一个下拉字段中选择的值。
例如:如果我们有 3 个选项(A、B、C)并且我们在下拉菜单 1 中选择 A,那么 A 应该在剩余的下拉菜单中被隐藏或禁用,并且应该相应地反映任何动态变化。
正在从数据库中获取下拉值并显示在列表中。
以下是html代码。
<select class="form-control" th:name="course1" th:id="course1">
<option th:value="Preference_1" style="color:#8F8B87" selected>Select Preference 1</option>
<option th:each="i : ${courseList}" th:value="${i.electiveCourseId}" th:text="${i.electiveName}" style="color:#000"></option>
</select>
<select class="form-control" th:name="course2" th:id="course2">
<option th:value="Preference_2" style="color:#8F8B87" selected>Select Preference 2</option>
<option th:each="i : ${courseList}" th:value="${i.electiveCourseId}" th:text="${i.electiveName}" style="color:#000"></option>
</select>
为上述问题寻找 jQuery 解决方案。
解决方案
我会为每个字段设置一个更改处理程序。https://api.jquery.com/change
然后在函数中,获取选中的值。最后,在其他下拉列表中找到该选项并将其赋予“禁用”属性。
推荐阅读
- mongodb - Mongodb mongoose查询通过引用获取文档数
- python - python 在分区列结果之间剪切
- ssl - 如何将证书颁发机构 (CA) 添加到 k8s?
- c# - .NET Core API - 从服务器而不是本地存储返回图像 URL
- spring-boot - 使用 h2 数据库时如何“允许远程创建数据库”?
- spring-boot - Redis 无法序列化/反序列化内部 Map 对象
- rust - 结构所有权
- r - 带有 Mapdeck 的自定义标记
- r - 在 Rproj 中使用某些脚本打开 Rstudio
- ios - Xcode 如何获取代码中所有拼写错误的单词的列表?