【问题标题】:Call js function in text using Thymeleaf使用 Thymeleaf 在文本中调用 js 函数
【发布时间】:2021-09-06 02:39:15
【问题描述】:

我有这个js函数

import numberToWordsRu from 'number-to-words-ru';
function convert(number)
{
    return numberToWordsRu.convert(number);
}

我想在 th:text 中调用它来传递参数。像这样的:

<p th:text="'This ' + ${param} + ' is ' + convert(${param})">
</p>

是否有可能以及如何正确地做到这一点?

【问题讨论】:

  • 这可能工作 th:onclick="'myFunction() 你可以参考stackoverflow.com/questions/26526037/…
  • 这可能有效 th:onclick="'myFunction() 你可以参考stackoverflow.com/questions/26526037/…
  • 了解 Thymeleaf 在服务器上运行很重要。它从模板生成 HTML,然后将 HTML 发送到显示它的浏览器。到那时,JavaScript 可以开始工作,但 Thymeleaf 将不再做任何事情。
  • 这就是我想要的,我想在开始时使用函数更改输入值。

标签: javascript html thymeleaf


【解决方案1】:

th:text 属性评估它的值表达式并将评估结果设置为它所在的标记的主体。不能将 javascript 函数与th:text 一起使用

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 2020-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多