【问题标题】:What is the correct syntax for location.hreflocation.href 的正确语法是什么
【发布时间】:2019-09-07 03:32:52
【问题描述】:

我一直在试图弄清楚如何通过 th:onclick 按钮将 url 传递给 spring 控制器方法。我已经尝试了许多不同的变体,但无法弄清楚我应该拥有多少 ' 或者我是否正确地逃避了事情。有人可以解释为什么这不起作用/使用 location.href 的正确方法是什么?

目前这是错误:无法解析为表达式:“'window.location.href=\'/removeContact''”

谢谢!!

 <button type="button" th:onclick="'window.location.href=\'/removeContact''" name="removeContact" th:value="${stat.index}" class="btn btn-danger trashContact"><span class="fa fa-trash"></span></button>

【问题讨论】:

  • 你不应该使用锚点吗?
  • 您将语句包装在 " 和 ' 中。删除 '。只需使用 th:onclick="window.location.href='/removeContact'"

标签: javascript spring-boot thymeleaf


【解决方案1】:

${..} 是 Thymeleaf 中的变量表达式,也是拼写。

所以使用two single quotes in spel expression for escape single quote.

th:onclick="'window.location.href=${'''/removeContact'''}'"

并在Thymeleaf standard expression text literal 中使用反斜杠和单引号。

th:onclick="'window.location.href=\'/removeContact\''"

【讨论】:

  • 您列出的第二个选项@gilchris 是唯一对我有用的选项。谢谢!
猜你喜欢
  • 1970-01-01
  • 2014-04-19
  • 2018-11-14
  • 1970-01-01
  • 1970-01-01
  • 2023-01-25
  • 1970-01-01
  • 2018-11-22
相关资源
最近更新 更多