javascript - thymeleaf : create URL with parameters

标签 javascript html thymeleaf

我在 Thymeleaf 模板中有这个函数来创建 URL

/company/delete/id

有了这个功能

function confirmDelete (id) {
        var r = confirm("Are you sure to delete the item #" + id + " ?");
        if (r == true) {
            var link = /*[[@{/company/delete/}]]*/ + id;
            window.location.href = link;
        } 
}

但是 id 被忽略,生成的 URL 是没有 id 的 /company/delete/

最佳答案

当您像这样注释掉 Thymeleaf/javascript 时,Thymeleaf 正在尝试创建 JavaScript natural templates work 。它会删除评论结束后的所有内容,因为它认为您正在尝试使模板工作,既使用 Thymeleaf 进行处理,又只是在浏览器中查看它而不进行处理。

您应该只移动要附加 id 的位置。

function confirmDelete (id) {
  var r = confirm("Are you sure to delete the item #" + id + " ?");
  if (r == true) {
    var link = /*[[@{/company/delete/}]]*/ "";
    window.location.href = link + id;
  } 
}

关于javascript - thymeleaf : create URL with parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44256118/

相关文章:

javascript - jQuery/JS 包含运算符或类似的

javascript - 正则表达式有时返回结果,有时在类似字符串上返回 null

javascript - 使用 javascript 的 GoogleAppEngine JSON 对象

html - 滚动内容中的特定固定 div

java - 如何在 Spring + Thymeleaf 上添加弹出窗口

javascript - 访问标签内的文本

javascript - 如何将渐变的底部锚定到它在 HTML5 Canvas 中填充的容器?

javascript - jquery克隆一个没有文本内容的html结构

java - 表情符号在 ajax 后在 MySQL 表列和 HTML 中呈现,但在页面重新加载后不会呈现。为什么?

java - Thymeleaf 在 html 代码中插入文本?