最近做Springboot的项目,在使用Thymeleaf模板时使用了th:onclick属性来调用JavaScript方法,其中涉及到ajax传入UUID到后台异步更新数据的功能。然而我的写法是这样的:
<button class="btn btn-primary btn-xs" th:onclick="'javascript:audit(\''+${items.id }+'\');'" >通过审核</button>
因为我的id是UUID类型,所以也就是字符串,结果就报错了,然后上网找原因,结果发现是版本问题:
thymeleaf 的 th:onclick 引用的函数参数的入参是 model值的时候注意写法了。3.0.10 开始就变了。
THYMELEAF 3.0.10更改日志
*改进了HTML事件处理程序属性(th:on ),以允许将其值作为内联JavaScript的片段(使用JAVASCRIPT模板模式)进行处理。
以前写法(请放弃):
方式一:
<button class="btn" th:onclick="'audit(\'' + ${items.id} + '\');'">通过审核</button>
方式二:
<button class="btn" th:onclick="'audit(' + ${items.id} + ');'">通过审核</button>
方式三:
<button th:onclick="|audit(${items.id} )|">通过审核</button>
现在的写法:
<button class="btn" th:onclick="audit([[${items.id}]]);">通过审核</button>
原文:https://blog.csdn.net/u010592926/article/details/86373871