英文:
Thymeleaf: calling JavaScript function with th:onclick - 'Uncaught ReferenceError'
问题
Thymeleaf
>JavaScript: "Uncaught ReferenceError: getMenuResults is not defined at HTMLAnchorElement.onclick"
Code:
<div>
<a
th:each="menu : ${menus}"
th:onclick="getMenu([[${menu.dictId}]])"
th:text="${menu.dictName}"
></a>
</div>
<script>
function getMenu(menuId) {
console.log('menuId')
}
</script>
I want to call the JavaScript function using th:onclick, but every time the error "Uncaught ReferenceError: getMenuResults is not defined at HTMLAnchorElement.onclick" occurs. I tried everything on the internet, tried to use other answers on StackOverflow, but the error did not disappear.
英文:
Thymeleaf
>JavaScript: "Uncaught ReferenceError: getMenuResults is not defined at HTMLAnchorElement.onclick"
Code:
<div>
<a
th:each="menu : ${menus}"
th:onclick="getMenu([[${menu.dictId}]])"
th:text="${menu.dictName}"
></a>
</div>
<script>
function getMenu(menuId) {
console.log('menuId')
}
</script>
I want to call javascript function using th:onclick, but every time error "Uncaught ReferenceError: getMenuResults is not defined at HTMLAnchorElement.onclick" is occuring.
I tried everything on internet, tried to use another answers in StackOverflow, but error did not disappear.
答案1
得分: 0
我删除并重新克隆了项目,然后它开始工作。这是IDE中的问题。这就是我遇到这个错误的原因。
英文:
I deleted and cloned the project again, and it started to work. It was a problem in IDE. That is why I got this error.
答案2
得分: -1
你可以通过添加 onclick 属性直接访问以下菜单:
<div>
<a
th:each="menu : ${menus}"
th:attr="onclick=|javascript:getMenu('${menu.dictId}')|"
th:text="${menu.dictName}"
></a>
</div>
英文:
You can directly access menu as below by adding onclick attribute
<div>
<a
th:each="menu : ${menus}"
th:attr="onclick=|javascript:getMenu('${menu.dictId}')|"
th:text="${menu.dictName}"
></a>
</div>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论