我想做的是在单击该功能时将特定功能的ID传递给HTML输入元素。
到目前为止,我有这个:
function onFeature(feature, layer) {
layer.on({
click: document.getElementById('id_address').value = parseInt(feature.properties.pk)
});
}
$.ajax({
type:"GET",
url: "../geojson",
dataType: 'json',
success: function(response){
layer = L.geoJson(response, {
style: function(feature){
return {color: "#2c65c1", fillColor: "4774bc", weight: 2};
},
onEachFeature: onFeature
}).addTo(map);
}
});
但是,当我单击 map上的某个功能时,控制台出现以下错误:
TypeError: i[n].action.call is not a function
有人可以解释这里发生的情况吗?
解析工作正常,因此传递给HTML输入的值是一个数字。
您设置Leaflet click事件的语法不太正确。另外,您需要定义事件,以便它动态地获取单击的图层的必需属性...
function onFeature(feature, layer) {
layer.on('click', function(e) {
document.getElementById('id_address').value = parseInt(e.target.properties.pk)
});
}