当我用jsp做一个项目时,我尽量保持简单。现在我想在 Thymeleaf 中做项目。我想在删除之前显示一个确认对话框。我做到了。但是当我单击“确定”时,出现错误。我认为我无法在 onclick
方法内正确设置链接。
这是我的 html 代码:
<tr th:each="student,iterStat : ${studentList}">
<td th:text="${student.id}"></td>
<td th:text="${student.roll}"></td>
<td th:text="${student.firstName}"></td>
<td th:text="${student.lastName}"></td>
<td th:text="${student.age}"></td>
<td>
<!-- <a th:href="@{/deleteStudent/(id=${student.id})}">Delete</a> -->
<a href="#" onclick="confirmRemoveQuestion('@{/deleteStudent/(id=${student.id})}')">
Delete
</a>
</td>
<script>
function confirmRemoveQuestion(link) {
if (show_confirm()) {
window.location = link;
this.hide();
} else {
this.hide();
}
}
function show_confirm() {
return confirm("Are you sure you want to do this?");
}
</script>
</tr>
这是我的 Controller :
@RequestMapping(value = "/deleteStudent", method = RequestMethod.GET)
public String deleteStudent(@RequestParam(name="id") int id) {
studentService.deleteStudent(id);
return "redirect:/getStudents";
}
注意:没有对话框我可以轻松删除对象。该代码位于注释行中。
最佳答案
我终于把它修好了。我已经更换了线路
<a href="#" onclick="confirmRemoveQuestion('@{/deleteStudent/(id=${student.id})}')"> Delete</a>
与
<a href="@{/deleteStudent/(id=${student.id})}" onclick="return confirm('Are you sure you want to delete this item?');">Delete</a>
我已经删除了 javascript
代码。因为不再需要它了!
关于java - 如何在 thymeleaf 的 onClick 方法中设置链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50315506/