Thymeleaf访问DOM元素属性

时间:2019-06-17 17:30:10

标签: spring-boot dom thymeleaf

我在thymeleaf 3 webapp项目中使用SpringBoot 2.1.3。我想解决在表单中input type selectdisabled并且所选数据未通过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来解决此问题,但实际上不是很好的解决方案。

你能帮我吗?

谢谢

2 个答案:

答案 0 :(得分:0)

无法读取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>
    

答案 1 :(得分:0)

非常感谢您的支持。我怀疑我无法使用Thymeleaf来做到这一点,但是我不想创建具有选定值的JQuery inline elements

现在,我正在尝试一种简单的解决方法:

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

它仍然是不正确的,它在***行上引发了错误,我正在搜索正确的sintax以在我的select input中找到所有form,并在submit之前立即重新启用它form并评估其是否可行。

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

相关问题