gpt4 book ai didi

Javascript : missing ) after argument list

转载 作者:行者123 更新时间:2023-11-30 15:16:52 25 4
gpt4 key购买 nike

我正在尝试调用一个在 Google map 上添加标记的 javascript 函数。

这里是我的代码:

   {% for entreprise in entreprises %}
<script> addMarker({{ entreprise.latitude }},{{ entreprise.longitude }},{{ entreprise.nom}})</script>
{% endfor %}

这里是我的功能:

function addMarker(latitude,longitude,nom)
{
var marker = new google.maps.Marker({
position: {lat: latitude, lng: longitude},
map: map,
label: nom,
title: nom
});

}

我得到一个错误:在参数列表之后缺少 )当我尝试像这样引用我的变量时:

<script> addMarker('{{ entreprise.latitude }}','{{ entreprise.longitude }}','{{ entreprise.nom}}')</script>

我得到错误:

addMarker() is not defined.

如何在 Twig 中调用我的函数?

非常感谢您的帮助。

最佳答案

1.

    {% for entreprise in entreprises %}
<script> addMarker({{ entreprise.latitude }},{{ entreprise.longitude }},{{ entreprise.nom}})</script>
{% endfor %}

你需要加上引号因为latitude , longtitudenom将有一个 php 字符串的输出,而不是作为 javascript 的变量,所以你猜测添加 '是对的。

(但还是在循环外右转<script>更好最好有这个

 <script>
{% for entreprise in entreprises %}
addMarker('{{ entreprise.latitude }}','{{ entreprise.longitude }}','{{ entreprise.nom}}')
{% endfor %}
</script>
  1. 你以前调用过你的函数吗?如果你在它之前声明你的函数,它当然会说如果没有定义。所以在调用函数之前尝试导入脚本 addMarker

你使用 jQuery() 吗?如果是这样你可以使用

$(document).ready(function(){});

所以你可能会得到

 <script>
$(document).ready(function() {
{% for entreprise in entreprises %}
addMarker('{{ entreprise.latitude }}','{{ entreprise.longitude }}','{{ entreprise.nom}}')
{% endfor %}
});
</script>

关于Javascript : missing ) after argument list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44335175/

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