javascript 变量作用域用作 php 变量
javascript variable scope usage to use as php variable
我想使用通过单击地图点 (mapbox) 获得的 javascript 变量(电子邮件地址)。我想以 html/php 形式插入此变量作为 mailto 地址。但是我花了几个小时试图从 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/
我想使用通过单击地图点 (mapbox) 获得的 javascript 变量(电子邮件地址)。我想以 html/php 形式插入此变量作为 mailto 地址。但是我花了几个小时试图从 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/