![](/img/trans.png)
[英]How to pass ExpressJS locals variable to an EJS template and use it as parameter for calling a JS function from onload HTML event
[英]How to pass parameter to js function from template parameter array
我在龙卷风模板表中生成了城市,并试图像上一列详细信息按钮一样
{% if globals().has_key('results') %}
{% for result in results %}
<tr>
<td>{{result['name']}}</td>
<td>{{result['citizens']}}</td>
<td style="width:100px; height:100px;">
<div class="ui-grid-b">
<a class="ui-block-a ui-icon-detail" onclick="showDetails('{{id}}',{{result['city_id']}});" data-role="ui-li-aside" data-icon="right_arrow" data-theme="a"></a>
</div>
</td>
</tr>
{% end %}
{% end %}
然后单击以加载另一个带有url参数的页面。
<script type="text/javascript">
function showDetails(id, city_id)
{
window.location = '/cities?id='+id+'&city_id='+city_id;
}
</script>
如何传递参数以显示详细信息功能? (我不能使用',我尝试用\\“,但是它不起作用。)结果是字典列表,其中包含键名称,公民,city_id。龙卷风模板使用{{}}访问传递的参数。
showDetails = function (id, city_id)
{
window.location = '/cities?id='+id+'&city_id='+city_id;
}
我认为这是简单的方法
大括号内的变量将直接输出(或字符串的字符串表示形式),因此取决于设置的内容。
例如,如果您有:
id = 'foo'
results = [{ 'city_id': 'bar' }]
然后将它们传递给模板,然后此行:
showDetails('{{id}}',{{result['city_id']}});
将输出如下(您可以通过查看源代码确认):
showDetails('foo',bar);
我猜想您会丢失{{result['city_id']}}
周围的引号,除非result['city_id']
是整数,在这种情况下应该可以使用。 在JavaScript中,单引号或双引号都可以。
但是,正如@Pete已经指出的那样,“具有单击功能,可在带有url参数的另一个页面上单击”是我们的href
属性。 您可以使用以下方法节省许多工作和潜在的故障:
<a href="/cities?id={{ id }}&city_id={{ result['city_id'] }}" ....></a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.