【问题标题】:How to set link in onClick method in thymeleaf?如何在百里香的onClick方法中设置链接?
【发布时间】:2018-08-08 02:07:23
【问题描述】:

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

这是我的控制器:

@RequestMapping(value = "/deleteStudent", method = RequestMethod.GET)
public String deleteStudent(@RequestParam(name="id") int id) {
    studentService.deleteStudent(id);
    return "redirect:/getStudents";
}

注意:没有对话框我可以很容易地删除一个对象。该代码在注释行中。

【问题讨论】:

    标签: java html spring thymeleaf


    【解决方案1】:

    我终于修好了。我已经换了行

    <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 代码。因为不再需要它!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多