【问题标题】:append attribute for the first element in thymeleaf百里香叶中第一个元素的附加属性
【发布时间】:2019-12-03 21:42:01
【问题描述】:

我尝试在 thymeleaf 3 中添加 required 当它是第一个元素时。我已经有了属性名。

<div th:each="role, iter : ${roles}" th:remove="tag">
    <input type="checkbox"  th:attr="name='roles[]'" th:attrappend="${iter.first ? 'required' : ''}"  th:value="${role.id}" th:text="${role.name}">
</div>

我收到此错误

org.thymeleaf.exceptions.TemplateProcessingException:无法解析 作为分配序列:“${iter.first ? 'required'}”

【问题讨论】:

    标签: thymeleaf


    【解决方案1】:

    这里不需要th:attrappend,只需使用th:required。我会这样格式化:

    <th:block th:each="role, iter : ${roles}">
        <input type="checkbox"  name="roles[]" th:required="${iter.first}" th:value="${role.id}" th:text="${role.name}" />
    </th:block>
    

    如果你想使用 th:attrappend,你必须用赋值来格式化,像这样:

    <th:block th:each="role, iter : ${roles}">
        <input type="checkbox"  name="roles[]" th:attrappend="required=${iter.first} ? 'required'" th:value="${role.id}" th:text="${role.name}" />
    </th:block>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-14
      • 2015-11-29
      • 1970-01-01
      • 1970-01-01
      • 2021-08-01
      • 1970-01-01
      相关资源
      最近更新 更多