在单击使用 for 循环创建的传单标记时给出操作

Giving actions on click of leaflet markers created using for loop

本文关键字:单标记 操作 创建 单击 for 循环      更新时间:2023-09-26

我试图将ajax函数调用作为对单击map中标记的响应。但是链接不起作用(可能是因为)在单击时,循环不再运行。

法典:

<script>
function load_map_module(query) {
    markers.clearLayers();
    $.ajax({
        url: '/load-map/',
        type: 'get',
        data: {'key':query.key, 'id':query.id},
        dataType: 'json',
        async: false,
        success: function(d) {
            res = d.res
            if (res.length > 0){
                for(i=0; i<res.length; i++) {
                    var marker = L.marker(new L.LatLng(res[i].latitiude, res[i].longitude), {icon: pinkicon, zIndexOffset:1000, riseOnHover:true, riseOffset:1000});
                    marker.bindPopup(res[i].name).openPopup();
                    markers.addLayer(marker);
                    temp_popup = (new L.Popup())
                        .setLatLng(new L.LatLng(res[i].latitiude, res[i].longitude))
                        .setContent("<span style='cursor:pointer;' <a href='#'> onclick="return load_map_module(({'key':'res.key', 'id':{{res.id}}}))">Hiii</a></span>");
                        .openOn(map);
                    marker_list.push(marker);
                    latlng_list.push(new L.LatLng(res[i].latitiude, res[i].longitude));
                }
            }
        }
    });
    map.addLayer(markers);
    return true;
}
</script>
弹出窗口

的内容 HTML 无效,span的开始标记未正确关闭,并且您打开a标记关闭了两次,一次是在 onclick 属性之前。此外,您在onclick处理程序中的函数调用完全错误,这不是连接字符串和变量的方法,并且您混合了双引号和单引号:

.setContent("<span style='cursor:pointer;' <a href='#'> onclick="return load_map_module(({'key':'res.key', 'id':{{res.id}}}))">Hiii</a></span>")
.setContent("<span style='cursor:pointer;'><a href='#' onclick='return load_map_module({key:" + res.key + ", id:" + res.id + "})'>Hiii</a></span>")

另外,为什么要将弹出声明放在括号中?

temp_popup = (new L.Popup())

这应该可以正常工作:

temp_popup = new L.Popup()