Thymeleaf:使用th:onclick调用JavaScript函数 – ‘Uncaught ReferenceError’

huangapple go评论55阅读模式
英文:

Thymeleaf: calling JavaScript function with th:onclick - 'Uncaught ReferenceError'

问题

Thymeleaf

>JavaScript: "Uncaught ReferenceError: getMenuResults is not defined at HTMLAnchorElement.onclick"

enter image description here

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"

enter image description here

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

&lt;div&gt;
  &lt;a
    th:each=&quot;menu : ${menus}&quot;
    th:attr=&quot;onclick=|javascript:getMenu(&#39;${menu.dictId}&#39;)|&quot; 
    th:text=&quot;${menu.dictName}&quot;
  &gt;&lt;/a&gt;
&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年5月22日 20:02:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305979.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定