湖上湖
2023-03-18 11:24:37
我想使用通過單擊地圖點 (mapbox) 獲得的 javascript 變量(電子郵件地址)。我想以 html/php 形式插入此變量作為 mailto 地址。但是我花了幾個小時試圖從 onclick 函數(shù)中獲取變量,但無濟(jì)于事。這是我最后一次嘗試: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"/> 謝謝您的幫助。
1 回答

拉莫斯之舞
TA貢獻(xiàn)1820條經(jīng)驗 獲得超10個贊
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">元素創(chuàng)建你的 html 文檔,你可以看到它工作
這是假設(shè)var agencyemail = e.features[0].properties.email; 實際上會為您提供所需的信息,我無法驗證這一點。
添加回答
舉報
0/150
提交
取消