java - 如何在 thymeleaf 的 onClick 方法中设置链接?

标签 java html spring thymeleaf

当我用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/

相关文章:

java - Spring MVC CSS/JS 未找到

java - 软件中的“独立交易”是什么?

java - Spring GenericFilterBean 中的异常处理

java - 在Android中获取间隔日期?

javascript - 需要在 javascript 中创建嵌套对象,链接 3 个用户输入数组以创建树状模式

spring - 如何使用 docker-compose.yml 在 Docker 上运行 Redis?

java - 为什么我的 while 循环需要这行代码才能运行?

javascript - 如何在 JavaScript 中查找目标 id?

javascript - 无法使用javascript更改图像的高度和宽度

java - 将应用程序正确划分为模块