gpt4 book ai didi

javascript - 传单:removeLayer() 在 JavaScript 中只工作一次

转载 作者:行者123 更新时间:2023-12-05 05:50:01 25 4
gpt4 key购买 nike

this is the my Map ,我在传单 map 中的 removeLayer 删除一次(remove Picture ),正如您在图片中看到的那样,它完美地删除了标记,但是当我再次尝试删除它时,它没有! check is not true but it didnt remove for 2nd time .这是从复选框调用的函数:

 function myFunction(id) {
var marker = [];
var checkBox = document.getElementById(id)
var lat = checkBox.name;
var lon = checkBox.value;
var RN = checkBox.className;
console.log(RN)

var pop_cont = `<img id="icon" src="sample.jpg" alt="shipPIC"/><br><b>${RN}</b><br> latitude :${lat}<br>longitude: ${lon}`
if (checkBox.checked == true) {
marker = L.marker([lat, lon]).addTo(map);
marker.setIcon(dish_icon);
marker.bindPopup(pop_cont).openPopup();
console.log("checked")

} else {
marker = L.marker([lat, lon])
var x_id = L.stamp(markers);
map.removeLayer(markers[id]);
console.log("NOOOOT Check");



}
}

非常感谢!

最佳答案

您检查复选框是否被选中。这意味着您已经以编程方式选中和取消选中该复选框。像那样。我注释掉了传单部分。

function myFunction(id) {
console.log(id)
var marker = [];
var checkBox = document.getElementById(id)
var lat = checkBox.name;
var lon = checkBox.value;
var RN = checkBox.className;
console.log(RN)

var pop_cont = `<img id="icon" src="sample.jpg" alt="shipPIC"/><br><b>${RN}</b><br> latitude :${lat}<br>longitude: ${lon}`
if (checkBox.checked == true) {
//marker = L.marker([lat, lon]).addTo(map);
//marker.setIcon(dish_icon);
//marker.bindPopup(pop_cont).openPopup();
console.log("checked")
checkBox.checked = false;
} else {
//marker = L.marker([lat, lon])
//var x_id = L.stamp(markers);
//map.removeLayer(markers[id]);
console.log("NOOOOT Check");
checkBox.checked = true;
}
}
<button onclick="myFunction('vehicle1')">klick</button>
<input type="checkbox" id="vehicle1" name="vehicle1" value="Bike">

关于javascript - 传单:removeLayer() 在 JavaScript 中只工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70589663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com