我在 Thymeleaf 页面中有这段代码,我想在其中获得类似 selectAuthorizedDriver1,selectAuthorizedDriver2,... 的内容
我尝试过
<tr th:each="item: ${devices}" >
<select th:id="selectAuthorizedDriver${item.id}" >
和
<tr th:each="item: ${devices}" >
<select id="selectAuthorizedDriver${item.id}" >
还有
<tr th:each="item: ${devices}" >
<select th:id="selectAuthorizedDriver${id}(id=${item.id})" >
但是我得到了错误:
Could not parse as expression: "selectAuthorizedDriver${item.id}"
或字面上的“selectAuthorizedDriver${item.id}”
最佳答案
对于只需要字符串的表达式,您可以将它们附加在一起。在您的示例中:
<tr th:each="item: ${devices}" >
<select th:id="${'selectAuthorizedDriver' + item.id}">
您还可以使用 preprocessing 来完成此操作,但在本例中不需要。 (如果您需要编写 th:field 表达式,则必须这样做)。
<tr th:each="item: ${devices}" >
<select th:id="${'selectAuthorizedDriver__${item.id}__'}">
关于javascript - thymeleaf 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48499509/