简体   繁体   English

th:onclick with javascript 函数调用一直说该函数未声明

[英]th:onclick with javascript function call keeps saying the function is not declared

Good afternoon.下午好。

My problem is that I'm trying to make a call from a button with the th:onclick tool, the function is in a .js file I've included in my html page, but everytime I try the button it says "deleteChargebox is not defined" in the browser console.我的问题是我正在尝试使用 th:onclick 工具从按钮进行调用,该函数位于我的 html 页面中包含的 .js 文件中,但是每次我尝试该按钮时,它都会显示“deleteChargebox 是未定义”在浏览器控制台中。

Here it is my .js file with only this function:这是我的 .js 文件,只有这个功能:

function deleteChargebox(idChargebox) {
    $.ajax({
        url: '/admin/chargeboxlist/delete/' + idChargebox,
        type: 'DELETE',
        success: function () {
            alert('El cargador ' + idChargebox + ' ha sido eliminado.');
            $("#table_chargeboxes").load('/');
        },
        error: function (error) {
            alert('Error, el cargador ' + idChargebox + ' no ha sido eliminado.');
        }
    });
}

Here the call I'm making:这是我正在拨打的电话:

<tr th:each="chargebox : ${chargeboxes}" th:id="${chargebox.idChargebox}">
    <td>
        <button class="btn btn-danger" id="chargeboxdeleteButton"
                th:onclick="'javascript:deleteChargebox(' + ${chargebox.idChargebox} + ')'">
            <span th:text="#{common.button.delete}"></span>
        </button>
    </td>
</tr>

And here how I'm inserting the script in the header:在这里,我如何在标题中插入脚本:

<script th:src="@{'/js/admin/chargeboxlist.js' + '?v=' + ${session.version}}"
            src="../static/js/admin/chargeboxlist.js"></script>

Does anyone knows why it keeps saying that the function is not defined and how to make the call?有谁知道为什么它一直说该函数未定义以及如何进行调用?

Thank you so much!非常感谢!

If onclick doesn't work then just select the element via document.getELementById() and apply a query selector.如果onclick不起作用,则只需通过document.getELementById()选择元素并应用查询选择器。 The Code编码

document.getElementById('the_id').querySelector('click', () => {
    // Do the following code here
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM