Javascript 在鼠标单击时加载传单数据并编辑要素特性

Javascript 在鼠标单击时加载传单数据并编辑要素特性,javascript,jquery,ajax,leaflet,Javascript,Jquery,Ajax,Leaflet,我从GeoJSON文件加载地图数据,并为每个文件附加一个单击事件。单击时,脚本应该从服务器获取数据并修改单击多边形的一个属性。i、 e: function onClick(e) { var status = e.target.feature.properties.ACCESS; $.ajax({ url: "http://127.0.0.1:8080/?&u=x&p="+e.target.feature.properties.ID_PARCELL,

我从GeoJSON文件加载地图数据,并为每个文件附加一个单击事件。单击时,脚本应该从服务器获取数据并修改单击多边形的一个属性。i、 e:

function onClick(e) {
    var status = e.target.feature.properties.ACCESS;
    $.ajax({
        url: "http://127.0.0.1:8080/?&u=x&p="+e.target.feature.properties.ID_PARCELL,
        dataType: 'jsonp',
        type: 'GET',
        success: function(data) {
        status = data.status;
        e.target.feature.properties.ACCESS = data.status;
        e.target.bindPopup("Owner: <b>"+ data.status +"</b>").openPopup();
        },
        error: function(data){console.log(data)}
    });
    e.target.feature.properties.ACCESS = status;
    map.fitBounds(e.target.getBounds());
}
但是由于success函数是否是一个回调同步函数,这并不重要,我无法返回到原始事件源e,以便修改它的一个属性


我的问题是:加载此数据后如何返回事件源?有没有通用的Javascript方法?如果没有,我是否可以通过功能ID查询GeoJSON层?=>因此,我可以在ajax调用中发送特性ID,并简单地通过响应将其取回

您在Click和success函数中使用相同的事件名称“e”。试着改变其中一个。即

function onClick(e) {
     var status = e.target.feature.properties.ACCESS;
     $.ajax({
         url: "http://127.0.0.1:8080/?&u=x&p="+e.target.feature.properties.ID_PARCELL,
         dataType: 'jsonp',
         type: 'GET',
         success: function(data, evt) {
             status = data.status;
             e.target.feature.properties.ACCESS = data.status;
             e.target.bindPopup("Owner: <b>"+ data.status +"</b>").openPopup();
        },
        error: function(data){console.log(data)}
     });
     e.target.feature.properties.ACCESS = status;
     map.fitBounds(e.target.getBounds());
 }
现在,您可以在返回结果时更改初始事件“e”属性。
希望这能有所帮助。

您在单击和成功功能上使用了相同的事件名称“e”。试着改变其中一个。即

function onClick(e) {
     var status = e.target.feature.properties.ACCESS;
     $.ajax({
         url: "http://127.0.0.1:8080/?&u=x&p="+e.target.feature.properties.ID_PARCELL,
         dataType: 'jsonp',
         type: 'GET',
         success: function(data, evt) {
             status = data.status;
             e.target.feature.properties.ACCESS = data.status;
             e.target.bindPopup("Owner: <b>"+ data.status +"</b>").openPopup();
        },
        error: function(data){console.log(data)}
     });
     e.target.feature.properties.ACCESS = status;
     map.fitBounds(e.target.getBounds());
 }
现在,您可以在返回结果时更改初始事件“e”属性。
希望这有帮助。

解决方案是使用上下文条目将所需变量e发送到匿名函数:

function onClick(e) {
    var status = e.target.feature.properties.ACCESS;
    $.ajax({
        url: "http://127.0.0.1:8080/?&u=x&p="+e.target.feature.properties.ID_PARCELL,
        dataType: 'jsonp',
        context: e,
        type: 'GET',
        success: function(data) {
        status = data.status;
        e.target.feature.properties.ACCESS = data.status;
        e.target.bindPopup("Owner: <b>"+ data.status +"</b>").openPopup();
        },
        error: function(data){console.log(data)}
    });
    e.target.feature.properties.ACCESS = status;
    map.fitBounds(e.target.getBounds());
}

解决方案是使用上下文条目将所需变量e发送到匿名函数:

function onClick(e) {
    var status = e.target.feature.properties.ACCESS;
    $.ajax({
        url: "http://127.0.0.1:8080/?&u=x&p="+e.target.feature.properties.ID_PARCELL,
        dataType: 'jsonp',
        context: e,
        type: 'GET',
        success: function(data) {
        status = data.status;
        e.target.feature.properties.ACCESS = data.status;
        e.target.bindPopup("Owner: <b>"+ data.status +"</b>").openPopup();
        },
        error: function(data){console.log(data)}
    });
    e.target.feature.properties.ACCESS = status;
    map.fitBounds(e.target.getBounds());
}

啊,不。。。我会改正的。。代码是错误的。。。我的错!!在成功的论证中没有e通过,我的意思是这是一些实验性的东西,我在这里发布之前没有真正清理。现在干净了。所以在成功中没有e传递。你有什么线索吗?啊没有。。。我会改正的。。代码是错误的。。。我的错!!在成功的论证中没有e通过,我的意思是这是一些实验性的东西,我在这里发布之前没有真正清理。现在干净了。所以在成功中没有e传递。你有什么线索吗?一旦你找到了解决方案,就把它贴出来,真是太好了。很少有人这么做-谢谢你。一旦你找到了解决方案,你就可以发布它了。很少有人这么做-谢谢。