【问题标题】:Why does Thymeleaf generate a wrong id?为什么 Thymeleaf 会生成错误的 id?
【发布时间】:2022-01-01 21:29:23
【问题描述】:

我使用 Thymeleaf 创建了一个表单。此表单包含一个文本字段和一个标签。但是 Thymeleaf 没有为标签的for 属性分配正确的 id(或者实际上它为文本字段分配了错误的 id)。

这是模板:

<form action="#" th:action="@{/test}" th:object="${nameBean}" method="post">
<div class="form-group" th:classappend="${#fields.hasErrors('name')}? has-error">
    <label class="control-label" th:for="${#ids.next('name')}">Name</label>
    <input type="text" class="form-control" placeholder="Enter your name" th:field="*{name}" />
</div>
<button type="submit" class="btn btn-default">Send</button>
</form>

这是生成的 HTML:

<form action="/test" method="post">
  <div class="form-group">
    <label class="control-label" for="name1">Name</label>
    <input type="text" class="form-control" placeholder="Enter your name" id="name" name="name" value="">
  </div>
  <button type="submit" class="btn btn-default">Send</button>
</form>

根据 Thymeleaf 指南,for 属性应该是正确的,但文本字段的 id 应该附加一个数字。为什么没有附加这个数字?

【问题讨论】:

    标签: thymeleaf


    【解决方案1】:

    据我了解,您只需要这个来进行迭代。通常你可以简单地使用for='name'。如果您使用 seq,我会假设输入元素处的 th:id=#ids.seq('name') 是正确的方式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-20
      • 2020-01-15
      • 1970-01-01
      • 1970-01-01
      • 2014-05-12
      • 2022-01-23
      相关资源
      最近更新 更多