通过单击使用for循环创建的传单标记来执行操作

时间:2015-01-20 13:16:56

标签: javascript jquery ajax google-maps leaflet

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

代码:

<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>

1 个答案:

答案 0 :(得分:0)

您的弹出窗口内容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()