我有一个下拉列表和一个表格,在下拉列表中应该列出所有类别为"软件"的对象(工作正常),在表格中应该列出所有类别为"硬件"的对象。但我的目标是,如果软件与硬件不兼容,则应禁用此硬件(灰色)。
所以我正在尝试访问"不兼容"列表,以便我可以检查硬件是否与下拉列表中的所选软件兼容。但我就是找不到解决方案来获取所选软件。
这是我的.html:
<select th:field="*{objects}" >
<option th:each="software : ${objects}"
th:if="${software.category} == software"
th:value="${software.id}"
th:text="${software.name}">
</option>
</select>
<table class="table">
<tr th:each="hardware: ${objects}"
th:if="${hardware.category} == hardware"
th:class="${#lists.contains([selectedSoftware].getIncompatible(), hardware)} ? disable : normal">
<td th:text="${hardware.id}"></td>
<td th:text="${hardware.name}"></td>
<td th:text="${hardware.category}"></td>
</tr>
</table>
首先,thymeleaf
是服务器端引擎。换句话说,除非用户提交表单,否则thymeleaf
不知道客户端当前在页面中执行的操作。我假设您正在提交表单,因为您已经使用了th:field
.
您没有正确使用th:field
。在这里,您将所选值绑定到整个对象数组。属性th:field
应指向<form>
中使用的th:object
(又名命令)中的字段。例如及以下:
<select th:field="*{selectedSoftwareId}" >
如果你正在寻找客户端解决方案,那么你需要使用Ajax和JavaScript。