gpt4 book ai didi

javascript - 将变量从 thymeleaf 传递给 javascript 函数?

转载 作者:行者123 更新时间:2023-11-30 15:00:38 27 4
gpt4 key购买 nike

我有一个 html 文件 thymeleaf,它有一个从 Controller 传递的变量,我需要将其提供给外部 javascript 文件上的函数。我该怎么做?

我可以像这样获取变量

<label th:utext="${id}" ></label>

我需要将该 id 传递给内部的函数

<script th:src="@{/js/myfunctions.js}" type="text/javascript"></script>

那里有一个功能:

function myFunction(id){

}

最佳答案

你可以这样做:

<input type="hidden" id="yourId" th:value="${id}"/>

然后在你的 js 函数中:

function myFunction(){
var val = $("#yourId").val();
}

请注意,我使用的是 Jquery,但原理是一样的。

如果 JS 函数代码在您的 html 页面中(不是 .js 外部文件),您可以像这样访问模型值:

function myFunction(){
var val = "${id}";
}

关于javascript - 将变量从 thymeleaf 传递给 javascript 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46586998/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com