spring - Thymeleaf 表格行点击不识别 js 功能
问题描述
onclick 事件抱怨未定义 java 脚本函数。
<body>
<div layout:fragment="content" th:remove="tag">
<div class="container">
<h6 class="mb-3">Restaurant Order Details</h6>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th scope="col">Order Id</th>
<th scope="col">Order Details</th>
<th scope="col">Date</th>
<th scope="col">Amount</th>
</tr>
</thead>
<tbody>
<tr th:each="order : ${orders}" style="cursor: pointer"
th:attr="onclick=|getOrderItems('${order.orderId}')|">
<td scope="row" th:text="${order.orderId}"></td>
<td th:text="${order.orderDetais}"></td>
<td th:text="${order.orderDate}"></td>
<td th:text="${order.amount}"></td>
</tr>
<tr>
<td colspan="3">Total</td>
<td th:text="${grandTotal}"></td>
</tr>
</tbody>
</table>
<div class="mb-4"></div>
</div>
</div>
<script>
function getOrderItems(orderId) {
alert("order details here");
}
</script>
</body>
当我点击表格行时,我得到
Uncaught ReferenceError: getOrderItems is not defined
at HTMLTableRowElement.onclick
当我用简单的警报替换 getOrderItems 时,会触发警报。我该如何解决?
解决方案
这里的问题是我在标签<script>
之外声明了我的。<div layout:fragment="content" th:remove="tag">
由于这是一个片段,因此 thymeleaf 仅渲染此范围内的所有内容。将脚本标签放在其中解决了这个问题。
推荐阅读
- vba - SetFocus 对面
- javascript - 闭包与正常功能的区别?
- wordpress - Wordpress 管理栏未显示在前端
- autodesk-viewer - 如何以编程方式在 Autodesk Forge 查看器中返回主页?
- excel - 从 SAP GUI 自动导出工作表
- python - 如何检查图像是否有任何文字?
- javascript - 如何从数据表中获取 select2 的值
- android - 如何在 Kotlin 中动态创建 KeyFrameSet
- c# - 如何在 C# Xamarin API 28 中使用带有回调的 PendingIntent 而不是带有 ConnectivityManager.ConnectivityAction 的 Intent?
- python - 堆叠 2-d numpy 数组以在 python 中获取 3-d 数组