簡體   English   中英

在 onclick 屬性中使用 thymeleaf 變量

[英]Using thymeleaf variable in onclick attribute

在我當前的 spring-boot 項目中,我對這個 html 代碼有一個看法:

<button type="button" class="btn btn-primary" onclick="upload()" th:utext="#{modal.save}"></button>

onclick屬性中,對 function upload()的調用應該有一個參數,該值存儲在 thymeleaf 變量${gallery}中。

誰能告訴我如何在上面的命令中使用表達式?

我已經嘗試過這個:

  • th:onclick="upload(${gallery)"

  • th:attr="onclick=upload(${gallery)"

這些都不起作用。

我用這種方法解決了這個問題:

th:onclick="|upload('${command['class'].simpleName}', '${gallery}')|"

這應該工作:

<button th:onclick="'javascript:upload(' + ${gallery} + ')'"></button>

thymeleaf 3.0.10 th:onclick thymeleaf變量不起作用

這應該工作:

th:attr="onclick=|upload('${gallery}')|" 

在 Thymeleaf 3.0.10 版本中

使用 [[]] 更容易,thymeleaf 去注入模板中的值

如果值是字符串,則不需要引號

<button th:data-id="${quartzInfo.id}" 
    th:onclick="del(this,[[${quartzInfo.id}]]" type="button">
</button>

事實上,這個也有效:

th:attr="onclick=${'upload('+gallery+')'}"

是的,為時已晚,但我希望這會幫助需要它的人

嘗試這個

th:onclick="'upload('+${gallery}+')'"

較舊的回復在新版本的3.0.10中不起作用。 現在你需要:

<button th:data-id="${quartzInfo.id}" 
    onclick="del(this,this.getAttribute('data-id'))" type="button">
</button>

如果你打算使用attr ,其中有一個字符串HTML將會崩潰,你必須執行以下操作,以防字符串值。

th:attr="onclick=${'toggleContractLine('+ ''' + contract.id + ''' + ')'}"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM