在 JavaScript 函数中递增 VTL 变量
<script>
var count_security = 0;
#set($count_security = 0)
function increment() {
count_security++;
#set($count_security = $count_security + 1)
alert(count_security);
alert($count_security);
}
</script>
<html>
<input type="button" onclick="increment" />
</html>
当我单击按钮调用上述函数时,“$count_security
”变量仅递增一次。它不会进一步增加。
如果我做错了什么,请帮助。
<script>
var count_security = 0;
#set($count_security = 0)
function increment() {
count_security++;
#set($count_security = $count_security + 1)
alert(count_security);
alert($count_security);
}
</script>
<html>
<input type="button" onclick="increment" />
</html>
When I call the above function on click of the button the "$count_security
" variable is incrementing only once. Its not incrementing further.
Please help if I am doing something wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您需要考虑两个上下文
因此,当渲染时,您将在 Velocity 引擎中执行1,它将执行递增 $count_security 的速度逻辑。这将作为文字值呈现到输出中。
var count_security 是一个 JavaScript CLIENT 变量,可以由客户端更改和更新。
您的速度 #set() 代码不会作为“集合”呈现到输出中。 #set 是一个 VTL 函数,不会改变输出流。
我希望这是有道理的。
This is because of you have two contexts to consider
So when this renders you will have 1 execution in the Velocity engine which will execute the velocity logic that increments $count_security. This will be rendered as a literal value into the output.
the var count_security is a JavaScript CLIENT variable which can be altered and updated by the client.
Your velocity #set() code will not be rendered into the output as a "set". #set is a VTL function and does not alter the output stream.
I hope that makes sense.