在 th:onsubmit thymeleaf 中添加变量 [英] Add Variable inside th:onsubmit thymeleaf
问题描述
我正在尝试在 th:onsubmit 中添加变量
I'm trying to add variable inside th:onsubmit with
th:onsubmit="return confirm('Hi '" + ${user.name} + "')"
但它总是让我出错
格式错误的标记:属性+"在元素中出现多次
Malformed markup: Attribute "+" appears more than once in element
我在 thymeleaf 官方文档上也找不到 onsubmit 的例子
also i can't find onsubmit example on thymeleaf official document
推荐答案
onsubmit
没有什么特别之处,这就是为什么官方文档中没有关于它的任何内容——你只是没有格式化表达正确.我会像这样格式化表达式:
There is nothing special about onsubmit
which is why there is nothing in the official documentation about it -- you're simply not formatting the expression correctly. I would format the expressions like this:
th:data-username="${user.name}"
onsubmit="return confirm('Hi ' + this.getAttribute('data-username'))"
(为了避免安全错误,Thymeleaf 可能会禁止您直接在 JavaScript 中连接字符串,这就是我将其分离为它自己的属性的原因.)
(To avoid security errors, Thymeleaf may prohibit you from concatenating strings directly in your JavaScript, which is why I'm separating it out to it's own attribute.)
这篇关于在 th:onsubmit thymeleaf 中添加变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!