从thymeleaf

时间:2018-05-25 13:38:18

标签: javascript spring-mvc model-view-controller thymeleaf

我正在写一个基于Thymeleaf的项目,我遇到了这样的问题。我有一个应该在后端发送的表单。但在发送之前,我必须使用JS的肝脏执行一些计算。所以我决定做那样的事情:

   <form th:action= "'javascript:sendSearchRequest('this.form', '+${currentPage}+', '+${selectedPageSize}+')"

所以在这种情况下似乎是不可能的,因为无法处理this.form。然后我尝试了另一种方法:

 <button id="searchButton" class="btn btn-default" type="submit"
                        onclick="return sendSearchRequest(this.form, this.currentPage, this.selectedPageSize)">Search
 </button>

这次我无法使用currentPage和selectedPageSize参数,因为我只能从thymleaf操作符调用它们,例如th:onclick。

所以这是我的问题:是否可以从我的示例中的$ {currentPage}模型中发送表单参数和一些参数

1 个答案:

答案 0 :(得分:0)

您可以将它们作为字段添加到表单中(并在<input type="hidden" th:value="${currentPage}" id="currentPage" /> <input type="hidden" th:value="${selectedPageSize}" id="selectedPageSize" /> 中访问它们)。

<form th:data-current-page="${currentPage}" th:selected-page-size="${selectedPageSize}" ... 

或作为表单标记上的数据属性。

<button id="searchButton" class="btn btn-default" type="submit" th:onclick="|return sendSearchRequest(this.form, ${currentPage}, ${selectedPageSize})|">
    Search
</button>

我认为您也可以修复按钮点击,如下所示:

implode