javascript - 为什么按钮中的 th:attr 没有发送正确的数据?
问题描述
<th:block th:each="fh : ${datacenterFisicHosts}">
<div>
<tr class="row">
<td id="fisicHostName" th:text="${fh.name}"></td>
<td id="fisicHostIp" th:text="${fh.ip}"></td>
<td id="fisicHostOS" th:text="${fh.operatingSystem}"></td>
<td id="fisicHostNotes" th:text="${fh.notes}"></td>
<td>
<button class="credentialsButton btn btn-default btn-sm" th:attr="data-fisic-host-id=${fh.id}">CREDENCIALES</button>
</td>
<td>
<button class="btn btn-default btn-sm btn-deleteFH" th:attr="data-fhId=${fh.id}">
<span class="glyphicon glyphicon-remove-sign"></span> Eliminar
</button>
</td>
</tr>
</div>
</th:block>
</table>
这很奇怪,因为这是有效的:
<button class="credentialsButton btn btn-default btn-sm" th:attr="data-fisic-host-id=${fh.id}">CREDENCIALES</button>
这不是:
<button class="btn btn-default btn-sm btn-deleteFH" th:attr="data-fhId=${fh.id}">
<span class="glyphicon glyphicon-remove-sign"></span> Eliminar
</button>
这是不工作问题的 JS 代码:
$('.btn-deleteFH').click(function() {
var fisicHostId = $(this).data('fhId');
console.log(fisicHostId);
});
当我单击按钮时,我在控制台中得到一个“未定义”。
我究竟做错了什么 ?
解决方案
jQuerydata()
仅适用于以data-
将其更改为
th:attr="data-fhId=${fh.id}"
你也不能this
在箭头函数中使用,因为箭头函数没有明确的this
改变
$('.btn-deleteFH').click( () => {
至
$('.btn-deleteFH').click(function() {
推荐阅读
- angular - Angular 7 中的 i18n-iso-countries 根据当前语言显示国家/地区
- python - 我想检查字段是否以 .mov 结尾的 Zapier Python zap
- arrays - 来自字符串的bash数组仅保留分隔符之间的内容
- flask-sqlalchemy - 为什么当我将 SQLALCHEMY_DATABASE_URI 设置为“sqlite:///:memory:”时,我将其设置为配置中的路径?
- google-cloud-run - 如何在谷歌云运行中使用存储保险丝?
- javascript - 如何在 JavaScript 中创建一个简单的“悬停和变化”改进版本?
- anypoint-studio - 如何根据CSV中的第一列在Dataweave中实现分组
- javascript - 如何从 html 输入类型时间创建 javascript 日期对象?
- ios - 集成到 ios 应用程序时 Pnet 模型出现问题
- python-3.x - 缩放 PDF 以在峰值显示 100%