传递参数里面的按钮onclick函数里面的小册子中的bindPopUp
我想在小册子中的bindPop里面创建一个按钮监听器函数。但是it does not read parameter of onclick function
。在下面的代码alertConfirmed()function works fine
对我来说but
filterEventsBasedOnCluster(feature) does not read the parameter 'feature'
。它说功能未定义。 功能是一个对象。传递参数里面的按钮onclick函数里面的小册子中的bindPopUp
这里是代码:
layer.bindPopup('<div id="alert">Found...!<input type="button" value="Please confirm" onclick="alertConfirmed()"> <input type="button" id="create" value="see patients" onclick="filterEventsBasedOnCluster(feature)"><table id="table"></table></div>')
`
任何帮助深表感谢。
试试这个:
我只是仅校正错误部分:
onclick="filterEventsBasedOnCluster('+feature+')"
你是不是传递变量正确。
谢谢。它为我工作。但它说'意外的标识符'。它不占用双引号结尾,也就是('+ feature +')“ – user2953637
是的,我已经向你展示了处理方式,如果我解决了你的问题,它就是一个答案。 –
如果通过onclick
HTML attribute附加事件处理程序,则无法控制该处理程序收到的参数。让我引用文档:
传递给指定事件处理函数的单个参数是一个
MouseEvent
对象。在处理程序中,this
将成为触发事件的元素。
传递自定义参数的方法是定义闭包,方法是返回一个只接收事件引用的函数。
这与«Leaflet.contextmenu callbacks»和«Leaflet marker event fires at wrong time»的描述完全相同。
阅读。我是认真的。
那么到底它应该是这个样子:
function getHandlerForFeature(feat) { // A function...
return function(ev) { // ...that returns a function...
console.log(feat); // ...that has a closure over the value.
}
}
layer.bindPopup("<button id='mybutton'>Foo!</button>")
// The button doesn't exist in the DOM until the popup has been opened, so
layer.on('popupopen', function(){
L.DomEvent.on(
document.getElementById('mybutton'),
'click',
getHandlerForFeature(layer) // The result of this call is the event handler func.
);
});
注意,您可以不使用onclick="code"
语法,你需要创建的可运行代码串和该代码将只能访问全局范围内的变量。当然,您可以JSON.stringify()
您的数据,但您将无法引用外部变量。
目前,您正在将参数功能作为“功能”对象发送。如果您已在程序中将特征定义为变量对象,则应该像+ feature +一样发送它。 –