我想使用一个javascript变量(电子邮件地址(,它是通过单击映射点(mapbox(获得的。我想把这个变量作为mailto地址插入到一个html/php表单中。但我花了几个小时试图将变量从onclick函数中取出,但没有成功。这是我最后一次尝试:
var agencyemail;
map.on('click', 'unclustered-point', function (e) {
agencyemail = e.features[0].properties.email;
// the variable functions correctly within the onclick function... and then more
}
这显然不起作用。从这里,我想将捕获的变量插入到php表单中
$et_email_to =
'<script>
document.write(agencyemail);
</script>' ;
我也尝试过使用sessionStorage、localStorage或let,但我仍然没有弄清楚。谢谢你的帮助。
编辑:我使用了这里提出的解决方案。我只是简单地将变量插入到一个表单隐藏字段值中。
map.on('click', 'unclustered-point', function (e) {
agencyemail = e.features[0].properties.email;
document.getElementById("agencyemail").value = agencyemail;
和html
<input type="hidden" value="" id="agencyemail" name="agencyemail"/>
谢谢你的帮助。
map.on('click', 'unclustered-point', function (e) {
var agencyemail = e.features[0].properties.email;
// the variable functions correctly within the onclick function... and then more
// HERE is where you would use that variable to change something in a form, for example with jquery:
$('a.mailto').prop('href','mailto:' + agencyemail);
}
如果你用<a class="mailto">
元素创建html文档,你可以看到它在中工作
这是假设var agencyemail = e.features[0].properties.email;
实际上会给你想要的信息,我无法验证这一点。
向您展示jquery代码如何工作的微小代码片段:https://jsfiddle.net/jhc0ky1f/