在 javascript/Jquery 中使用 freemarker 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14682723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
use freemarker variable in javascript/Jquery
提问by Ahmad Beg
I have declared a variable in freemarker as
我在 freemarker 中声明了一个变量
<#assign myvariable= "value">
I want to access it in my javascript function like as follows
我想在我的 javascript 函数中访问它,如下所示
function myfunction(){
alert(myvariable);
}
回答by Deele
I guess, at first, you should output that variable into your HTML/JavaScript code, something like this:
我想,首先,您应该将该变量输出到您的 HTML/JavaScript 代码中,如下所示:
<script type="text/javascript">
var myvariable = "${myvariable}";
function myfunction(){
alert(myvariable);
}
</script>
回答by Sagar Kale
You can assign freemarker variable to HTML input field and access it in JavaScript using jquery or document Here how it is
您可以将 freemarker 变量分配给 HTML 输入字段,并使用 jquery 或文档在 JavaScript 中访问它这里是如何
<input id=“freemarkervar” type=“text or hidden” value=“${myVariable}”/>
<script type="text/javascript">
var val = $(“#freemarkervar”).val();
alert(val);
// using JavaScript
var val=document.getElementById("freemarkervar").value;
alert(val);
</script>
回答by jpllosa
You can use FreeMarker code in JavaScript straight away. Here's a sample code where FreeMarker supplies the data of a Morris.js chart. I think you'll get the idea.
您可以立即在 JavaScript 中使用 FreeMarker 代码。这是 FreeMarker 提供 Morris.js 图表数据的示例代码。我想你会明白的。
new Morris.Area({
element: 'searchTrend',
resize: true,
data: [
<#list searchCount as sc>
{day: '${sc.date!?string("yyyy-MM-dd")}', count: ${sc.searches}} <#sep>,
</#list>
],
xkey: 'day',
ykeys: ['count'],
labels: ['Count'],
xLabelFormat: function (x) { return x.getDate(); }
});

