Thymeleaf 访问 DOM 元素属性



我正在将thymeleaf 3SpringBoot 2.1.3 webapp项目一起使用。我想解决在表单内disabled input type select并且所选数据未通过 POST 发送时出现的问题。

我想在百里香叶页面中做类似的事情:

<select th:field="*{datiSocietariDto.provincia}" id="provincia" th:disabled="disabled">
  <option value="" selected>PROVINCIA</option>
  <option th:each="city : ${tipologicalDto.cities}" th:value="${city.descrizione}" th:text="${city.descrizione}"></option>
</select>
<span th:if=[provincia is disabled]>
  <input type="hidden" th:value=[value of selected option]>
</span>

我不知道如何在方括号内检索 te 信息,以及是否有可能用 thymeleaf 做这样的事情.我可以使用JQuery解决这个问题,但实际上不是很优雅的解决方案。

你可以帮我吗?

谢谢

无法读取 Thymeleaf 中另一个标签的禁用状态。您的选择是:

  1. 使用只读而不是禁用。 只读元素无法编辑,但仍会随帖子一起发送。

  2. 使用某种变量,例如:

    <th:block th:with="disabled=true">
        <select th:field="*{datiSocietariDto.provincia}" id="provincia" th:disabled="${disabled}">
          <option value="" selected>PROVINCIA</option>
          <option th:each="city : ${tipologicalDto.cities}" th:value="${city.descrizione}" th:text="${city.descrizione}"></option>
        </select>
        <input th:if="${disabled}" type="hidden" th:field="*{datiSocietariDto.provincia}">
    </th:block>
    

非常感谢您的支持。我怀疑我不能用Thymeleaf做到这一点,但我不想用选定的值创建JQuery inline elements

现在我正在尝试使用这样的简单解决方法:

$(".submit").click(function(){
  $(this).find(':select').attr('disabled', false); ***
  $('#msform').submit();
});

它仍然不正确,它在 *** 行上抛出了一个错误,我正在搜索正确的 sintax 以查找我form中的所有select input,并在submit form并评估它是否是一个可行的解决方案之前立即重新调整它。

无论如何,再次感谢您的快速和宝贵的支持。

最新更新