玩!模板 - 将动态字符串传递给 JavaScript 不起作用?
基本上,在控制器中,发生了一些动态的事情,我将一个非静态字符串传递给视图:
String token = "";
render(token);
我可以轻松地执行以下操作:
<div id="token>${token}</div>
...并获取内容:
$('#token').html();
但似乎不起作用的是,在 javascript 中,做:
function token(token) {
// do something with token
console.log('token: ' + token);
}
token(${token});
我可以理解为什么它不起作用......但是它的语法是什么?
Basically, in the controller, something dynamic happens, and I pass a non-static string to the view:
String token = "";
render(token);
I can easily do:
<div id="token>${token}</div>
...and grab the contents:
$('#token').html();
But what doesn't seem to work is, in the javascript, doing:
function token(token) {
// do something with token
console.log('token: ' + token);
}
token(${token});
I can kind of see why it doesn't work...but what is the syntax for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有命名您的函数。它应该是:编辑: 您可能只需要添加引号:
顺便说一句,这可行,但一般来说我会避免使用相同的名称对于函数及其参数。更好的名称可能是
logToken
:You didn't name your function. It should be:Edit: You may need to just add quotes:
By the way, this works, but in general I'd avoid using the same name for the function and its argument. A better name might be
logToken
: