如何使用Thymeleaf将对象属性传递给JavaScript函数?

2026-05-21 00:382阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计236个文字,预计阅读时间需要1分钟。

如何使用Thymeleaf将对象属性传递给JavaScript函数?

Thymeleaf 获取对象属性作为JavaScript函数的参数,分为以下两种情况:

1. 传递数值:`a rel=nofollow href=javascript:; th:onclick='changeStatus('+${user.id}+',1)'>启用`

2.函数调用:`function changeStatus(id, status) { alert(id + status); }`

如何使用Thymeleaf将对象属性传递给JavaScript函数?

Thymeleaf 获取对象属性作为JavaScript函数的参数分为以下两种情况:

传递数值

<a rel="nofollow" href="javascript:;" th:onclick="'changeStatus('+${user.id}+',1)'" >启用</a> function changeStatus(id,status){ alert(id+" "+status); }

传递字符串

<a rel="nofollow" title="删除" href="javascript:;" th:onclick="changeName(1,[[${user.name}]])" >修改名字</a> function changeName(id,name){ alert(id+" "+name); }

综合

<a rel="nofollow" href="#!" th:onclick="updateUser([[${user.id}]],[[${user.name}]],[[${user.addr}]],[[${user.priority}]],[[${user.status}]])" >编辑</a> function updateProvince(id,name,area,priority,status) { alert(id+" "+name+" "+area+" "+priority+" "+status) }

在JavaScript块中使用

<script type="text/javascript" th:inline="javascript"> let name= [[${user.name}]] </script>

本文共计236个文字,预计阅读时间需要1分钟。

如何使用Thymeleaf将对象属性传递给JavaScript函数?

Thymeleaf 获取对象属性作为JavaScript函数的参数,分为以下两种情况:

1. 传递数值:`a rel=nofollow href=javascript:; th:onclick='changeStatus('+${user.id}+',1)'>启用`

2.函数调用:`function changeStatus(id, status) { alert(id + status); }`

如何使用Thymeleaf将对象属性传递给JavaScript函数?

Thymeleaf 获取对象属性作为JavaScript函数的参数分为以下两种情况:

传递数值

<a rel="nofollow" href="javascript:;" th:onclick="'changeStatus('+${user.id}+',1)'" >启用</a> function changeStatus(id,status){ alert(id+" "+status); }

传递字符串

<a rel="nofollow" title="删除" href="javascript:;" th:onclick="changeName(1,[[${user.name}]])" >修改名字</a> function changeName(id,name){ alert(id+" "+name); }

综合

<a rel="nofollow" href="#!" th:onclick="updateUser([[${user.id}]],[[${user.name}]],[[${user.addr}]],[[${user.priority}]],[[${user.status}]])" >编辑</a> function updateProvince(id,name,area,priority,status) { alert(id+" "+name+" "+area+" "+priority+" "+status) }

在JavaScript块中使用

<script type="text/javascript" th:inline="javascript"> let name= [[${user.name}]] </script>