spring-boot - Thymeleaf : passing javascript parameters

标签 spring-boot thymeleaf

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

   <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,请尝试执行此操作

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

如果需要数字编号
th:onclick="'javascript:confirmDelete(' + ${company.id} + ');'"

关于spring-boot - Thymeleaf : passing javascript parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44090217/

相关文章:

java - 使用Spring Boot访问微服务

java - CORS和错误以及Access-Control-Allow-Origin header 的问题

spring - 无法转换类型 'java.lang.String' 的属性值

java - Springboot/Thymeleaf : Links on html-template cannot be parsed

java.lang.IllegalArgumentException : Cannot find a factory to create the request context

elasticsearch - spring-data-elasticsearch - Jackson 无法使用全局配置进行序列化

spring - Spring项目的Java 9 Jigsaw(模块)有什么计划吗?

java - 提交时表单给出 Null

spring-boot - react 环境中的 spring-cloud-sleuth

java - 在 thymeleaf 中调用函数时将模型值作为字符串类型包含在内