问题
I have e problem when click marker on my map leaflet and modal show only last data, not dynamically.
//Array
var koordinat = [
{id: 1, nama: 'Unesa', lat: -7.313047, lang: 112.727151},
{id: 2, nama: 'Coffe Warsalam', lat: -7.310355, lang: 112.732151},
{id: 3, nama: 'Kejaksaan', lat: -7.313824, lang: 112.733235},
];
//var sikat = [];
for(var i = 0; i < koordinat.length; i++) {
sikat = new L.Marker(new L.latLng(parseFloat([koordinat[i].lat]), parseFloat([koordinat[i].lang]))).addTo(mymap);
sikat.bindPopup(koordinat[i].nama).openPopup();
var namamu= koordinat[i].nama;
sikat.on('click', function() {
//$("#nama").html(koordinat[i].nama);
$('#Modal').modal('show').on('shown.bs.modal', function() {
var modal = $(this)
//Reset Isi Form
$('#info').trigger("reset");
modal.find('#nama').attr("value",namamu);
})
//$("#Modal").modal("show");
});
}
I want data show on current marker clicked. Thanks guys...
回答1:
Much probably a classic JavaScript scope issue with var
being hoisted whereas the developer does not expect it.
Replace var
in var namamu= koordinat[i].nama;
By const
: const namamu= koordinat[i].nama;
So that your namamu
variable become bound to the inner for
block scope.
来源:https://stackoverflow.com/questions/60657846/leaflet-onclick-data-not-dynamically