简体   繁体   English

从百里香发送参数到javascript

[英]Sending parameters to javascript from thymeleaf

I 'm writing a project based on Thymeleaf and I stuck with such a problem. 我正在写一个基于Thymeleaf的项目,但我遇到了这样的问题。 I have a form which is supposed to be send on back-end. 我有一个应该在后端发送的表格。 But before sending I have to perform some calculations with pages with the hepl of JS. 但是在发送之前,我必须使用js的hepl对页面进行一些计算。 So I decided to do something like that: 所以我决定做这样的事情:

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

So in this case it seems to be impossible, because this.form can not be processed. 因此,在这种情况下,这似乎是不可能的,因为无法处理this.form。 Then I tried another approach: 然后我尝试了另一种方法:

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

This time I cannot work with currentPage and selectedPageSize parameters, because I could only call for them only from thymleaf operators, like th:onclick for example. 这次我无法使用currentPage和selectedPageSize参数,因为我只能从百里香运营商那里要求它们,例如th:onclick。

So here is my question: is it possible to send both form parameter and some parameters from model like ${currentPage} in my example 所以这是我的问题:在我的示例中,是否可以同时发送表单参数和来自模型的某些参数,例如$ {currentPage}

You could add them as fields to your form (and access them in you sendSearchRequest ). 您可以将它们作为字段添加到表单中(并在sendSearchRequest访问它们)。

<input type="hidden" th:value="${currentPage}" id="currentPage" />
<input type="hidden" th:value="${selectedPageSize}" id="selectedPageSize"  />

or as data attributes on your form tag. 或作为表单标签上的数据属性。

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

I think you could fix your button click as well, to look like this: 我认为您也可以修复按钮单击,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM