小编LuN*_*rez的帖子

Leaflet.js:如何从地图中删除多个图层

我正在使用Leaflet.js作为地图.现在我想从地图中删除添加的图层.通过单击输入#button,所有选中的复选框将更改为未选中,并且所有相应的图层将从地图中删除.

要从地图中删除图层,需要图层的ID.此id等于相应复选框的ID.这就是为什么我使用jQuery来获取所有选中复选框的id并将它们的值存储在一个对象中,这里称为someObj.idsChecked.

当我尝试使用存储值val删除一个层时,它在console.log显示所需值时不起作用.这里例如:mapcat52.

将先前的id硬编码插入到像map.removeLayer(mapcat52)这样的函数中时,它按预期工作.

我的代码或我的想法中的错误在哪里?
任何帮助深表感谢.

HTML

<input type="button" id="selectnone" value="deselect all" />

<!-- checkboxes  --> 
<input id="mapcat52" type="checkbox" name="maplayer" class="maplayer"> 
<label for="mapcat52">Map Layer One</label>

<input id="mapcat53" type="checkbox" name="maplayer" class="maplayer"> 
<label for="mapcat53">Map Layer Two</label>

...
Run Code Online (Sandbox Code Playgroud)

JS:

$('#selectnone').click(function() {
    var someObj = {};
    someObj.idsChecked = [];

    $("input:checkbox").each(function() {
        if ($(this).is(":checked")) {

            someObj.idsChecked.push($(this).attr("id"));
        }
    }).attr('checked', false);

    $.each(someObj.idsChecked,function(id, val) {

          // displays the wanted value, e.g. "mapcat52" if checkbox with this id is checked
          console.log(val); …
Run Code Online (Sandbox Code Playgroud)

javascript checkbox maps jquery leaflet

8
推荐指数
2
解决办法
3万
查看次数

标签 统计

checkbox ×1

javascript ×1

jquery ×1

leaflet ×1

maps ×1