我正在尝试通过layer.bindPopup
来编写要素属性的功能以用于Leaflet功能。在这一点上,我几乎所有我需要的东西,除了最后一件事:Documentation是说layer.bindPopup
需要HTML字符串或HTML元素,所以我需要连接我的HTMLString
与两个元素:saveChanges
按钮和speed_input
输入,然后提供layer.bindPopup
与它。任何使用$.append
的操作都没有帮助。有关如何解决这个问题的任何建议?使用HTML字符串将HTML元素连接到Leaflet的bindPopup
function onEachArc(feature, layer) {
// Create an input
var speed_input = L.DomUtil.create('input', 'speed');
// Set a feature property as value
speed_input.value = feature.properties.speed;
// Add a listener to watch for change on time input
L.DomEvent.addListener(speed_input, 'change', function(){
// Change the value of speed
feature.properties.speed = speed_input.value;
});
// Bind popup to layer with input
HTMLString = '<table style="width:100%">\
<tr style="background-color:grey">\
<th><b>Arc Numer: </b>' + feature.properties.linkstr + '</br></th>\
</tr>\
<tr>\
<td><b>Speed: </b> ' + feature.properties.speed + '.</div></td>\
</tr>\
</table>';
var saveChanges = document.createElement('button');
saveChanges.innerHTML = 'Save Changes';
saveChanges.onclick = function(){
$.ajax({
type:"POST",
url:"php/updateFeature.php",
data: {feature: feature},
success: function(data){
$('#test').html(data);
}
});
//return false;
}
};
/*
This did not help
var box = document.createElement("div");
box.style.width = "100px";
box.style.height = "100px";
$("#box").append("#saveChanges");
layer.bindPopup(box);
*/
layer.bindPopup(saveChanges);
};
我更喜欢outerHTML方法,因为我觉得最终使用HTML字符串更容易。但是,如果我在此按钮上添加'onclick'事件,它将不起作用: 'button.onclick = function(e){e.preventDefault();警报(“嗨”)}' – ievgenii
这是哈克,但我只是回答你的问题,这不是一个很干净的方式来做这种事情。我会在我的回答中详细阐述一下。 – iH8
哇!这真的很花哨!我正在查看[this](https://www.mapbox.com/mapbox.js/example/v1.0。0/clicks-in-popups /)例子,并且我理解为什么我以前的方法不起作用的原因 - 我放入bindPopup的HTML还不存在,就像它在示例中解释的那样。但我想不出先创建HTML然后从'layer.on('click')'函数访问它的方法。非常感谢您的帮助! – ievgenii