Spring Thymeleaf th:onclick事件-调用确认函数
我是新来的。最近我遇到了以下情况。这是我的ThymileAF html页面的一部分:Spring Thymeleaf th:onclick事件-调用确认函数,spring,thymeleaf,Spring,Thymeleaf,我是新来的。最近我遇到了以下情况。这是我的ThymileAF html页面的一部分: <!-- an delete button link --> <a th:href="@{/employees/delete(employeeId=${tempEmployee.emplId},firstName=${tempEmployee.firstName},lastName=${tempEmployee.lastName})}" class="btn btn-danger bt
<!-- an delete button link -->
<a th:href="@{/employees/delete(employeeId=${tempEmployee.emplId},firstName=${tempEmployee.firstName},lastName=${tempEmployee.lastName})}"
class="btn btn-danger btn-sm py-1 "
th:onclick="if(!(confirm('Are you sure you want to delete this employee ?') )) return false" >
Delete
</a>
这段代码按预期工作良好。但是,我想添加员工姓名作为确认的一部分。代码如下:
<!-- an delete button link -->
<a th:href="@{/employees/delete(employeeId=${tempEmployee.emplId},firstName=${tempEmployee.firstName},lastName=${tempEmployee.lastName})}"
class="btn btn-danger btn-sm py-1 "
th:onclick="if(!(confirm('Are you sure you want to delete this employee ' + '\'+${tempEmployee.firstName}+\'' +'?' ) )) return false" >
Delete
</a>
不幸的是,结果是:
是否确实要删除此员工
“++${tempEmployee.firstName}+”
看起来Thymeleaf无法识别${tempEmployee.firstName}。它在th:href标记中没有问题,但不喜欢在th:onclick中使用
如果有人能把我引向正确的方向,我将不胜感激。不确定到底是什么问题(尽管它可能与
onclick
vsth:onclick
有关。无论如何,我认为这样的格式会起作用(还有一些额外的好处,比如没有JavaScript注入)
(注意我使用的是
onlick
而不是th:onclick
而不是上面代码中的这一行--onclick=“if(!confirm(this.getAttribute('data-confirm-delete'))return false”>
你可以这样写:
onclick=“返回确认(this.getAttribute('data-confirm-delete')”
<!-- an delete button link -->
<a
th:href="@{/employees/delete(employeeId=${tempEmployee.emplId},firstName=${tempEmployee.firstName},lastName=${tempEmployee.lastName})}"
class="btn btn-danger btn-sm py-1 "
th:data-confirm-delete="|Are you sure you want to delete this employee ${tempEmployee.firstName}?|"
onclick="if (!confirm(this.getAttribute('data-confirm-delete'))) return false"
>
Delete
</a>