Thymeleaf:传递javascript参数 [英] Thymeleaf : passing javascript parameters

查看:36
本文介绍了Thymeleaf:传递javascript参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的 SpringBoot 应用程序.使用 Spring Initializer、嵌入式 Tomcat、Thymeleaf 模板引擎,并打包为可执行 JAR 文件.我想将 POJO 的属性传递给 javascript 函数:

I have a basic SpringBoot app. using Spring Initializer, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file. I want to pass an attribute of a POJO to a javascript function:

   <tr th:each="company: ${companies}" >                                                
         <td class="col_actions">
           <a th:href="@{/company/edit/{id}(id=${company.id})}" style="color:#808080; margin-right: 10px;">
             <i class="fa fa-pencil-square-o" aria-hidden="true"></i>
           </a>
           <a href="#" style="color:#808080;  text-align: center;" onclick="javascript:confirmDelete ({id}(id=${company.id}));">
              <i class="fa fa-times" aria-hidden="true" ></i>
            </a>
         </td>
   </tr>

但是我得到一个错误:Uncaught SyntaxError: missing ) after argument list

推荐答案

问题出在anchor上,如果confirmDelete()函数需要字符串id,请试试这个方法

The problem came from anchor, so please try this one if the confirmDelete() function require a string id

th:onclick="'javascript:confirmDelete(\'' + ${company.id} + '\');'"

如果它需要一个号码 id

And if it need a number id

th:onclick="'javascript:confirmDelete(' + ${company.id} + ');'"

这篇关于Thymeleaf:传递javascript参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆