gpt4 book ai didi

javascript - 将 Grails 参数传递给 Javascript

转载 作者:行者123 更新时间:2023-11-28 21:00:42 27 4
gpt4 key购买 nike

我想将 GSP 中使用的 URL 参数传递给 jQuery 函数。我找到了一个可行的解决方案,但我的直觉告诉我使用全局 Javascript 变量不是一个好主意。

GSP - 使 Grails 中的 params.id 可作为 Javascript 的全局变量:

<g:javascript >
var categoryId = ${params.id}
</g:javascript>

在 jQuery 函数中使用此变量:

<g:javascript src="views/visual.js"/>

Javascript:

// Call with URL using the global variable defined in the GSP
$.getJSON(
"../visualJson?id=" + categoryId,
function (data) {
// Some code.
});

Grails 的惯用方法是什么?

最佳答案

虽然不一定特定于 Grails,但为什么不将变量传递到函数中呢?说:

在“views/visual.js”中,您有:

var someFunction = function(categoryId)
{
// Call with URL using the global variable defined in the GSP
$.getJSON(
"../visualJson?id=" + categoryId,
function (data) {
// Some code.
});
}

因此,您可以通过在应用中调用函数 someFunction('${params.id}') 来传递该值。

关于javascript - 将 Grails 参数传递给 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11134466/

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