3 回答
TA贡献1869条经验 获得超4个赞
如果你的
'<a href="#" id = "delete" data-value = id >delete</a>'
以某种方式结束delete()
在某处调用函数并向其传递data-value
属性,您可能需要做的就是编写它,以便使用定义时的 ID 的实际值:
'<a href="#" id = "delete" data-value = "' + id + "' >delete</a>'
TA贡献1871条经验 获得超8个赞
这样你就可以在弹出窗口中找到删除功能
marker.on('popupopen', function(e) {
// your delete function
});
更新示例
let config = {
minZoom: 7,
maxZomm: 18,
};
const zoom = 16;
const lat = 52.2297700;
const lon = 21.0117800;
let points = [
[52.230020586193795, 21.01083755493164, 'point 1'],
[52.22924516170657, 21.011320352554325, 'point 2'],
[52.229511304688444, 21.01270973682404, 'point 3'],
[52.23040500771883, 21.012146472930908, 'point 4']
];
const map = L.map('map', config).setView([lat, lon], zoom);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
// loop that adds many markers to the map
for (let i = 0; i < points.length; i++) {
const lat = points[i][0];
const lng = points[i][1];
const popupText = `<button data-value="test-${i+1}" class="delete">${points[i][2]}</button>`;
marker = new L.marker([lat, lng])
.bindPopup(popupText)
.addTo(map);
marker.on('popupopen', function(e) {
const btns = document.querySelectorAll('.delete');
btns.forEach(btn => {
btn.addEventListener('click', () => {
alert(btn.getAttribute('data-value'));
})
})
});
}
* {
margin: 0;
padding: 0
}
html {
height: 100%
}
body,
html,
#map {
height: 100%;
margin: 0;
padding: 0
}
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js"></script>
<div id="map"></div>
TA贡献1963条经验 获得超6个赞
最终代码:
最后我设法将所有内容拼凑在一起,getUsers()进行一些调整:
function getUsers() {
$.getJSON("getData.php", function (data) {
for (var i = 0; i < data.length; i++) {
var location = new L.LatLng(data[i].lat, data[i].lng);
var id = data[i].id;
var species = data[i].species;
var diameter = data[i].average_diameter;
var quality = data[i].quality;
var damage = data[i].damage;
var notes = data[i].additional_information;
var popupText = `<button data-value= "${data[i].id}" class="delete">Delete</button>`;
var marker = new L.marker([data[i].lat, data[i].lng], {icon: greenIcon}).addTo(map);
marker.bindPopup("ID:"+ id + "<br>" + "Species: " + species + "<br>" + "Diameter: " + diameter +"cm" + "<br>" +"Quality: " + quality + "<br>" +"Damage: " + damage + "<br>" +"Notes: " + notes + "<br>" + "<br>" + popupText);
marker.on('popupopen',function(e){
const btns = document.querySelectorAll('.delete');
btns.forEach(btn => {
btn.addEventListener('click', () => {
var id = btn.getAttribute('data-value');
$.ajax({
type: 'POST',
url: 'delete.php',
data: {id1:id},
success: function(data){
alert(data);},
error: function(data){
alert('Something went wrong.');}
});
})
});
});
}
})
}
并且还delete.php进行了一些调整:
<?php
$id= $_POST['id1'];
$connect = mysqli_connect("localhost", "root", "", "useraccounts");
$sql = "DELETE FROM geomarker WHERE id = ($id)";
$result = mysqli_query($connect, $sql);
if($result){
echo 'Data successfully deleted.';
}else{
echo 'There were erros while deleting the data.';
}
?>
感谢您的帮助!
- 3 回答
- 0 关注
- 105 浏览
添加回答
举报