【问题标题】:click event on leaflet popup content传单弹出内容上的点击事件
【发布时间】:2019-02-18 10:07:59
【问题描述】:

我正在尝试访问传单弹出窗口中的内容。具体来说,我添加了一个带有我想访问的按钮的表单。但现在我只是尝试向弹出窗口本身添加一个事件。

$(".leaflet-popup-content-wrapper .leaflet-popup-content").click(function(e) {
  alert("clicked");
});

LeafletJs 弹出标记示例:

<form class="popup-form">
  <div class="form-group">
    <label class="mb-0" for="comment">Comment:</label>
    <textarea class="form-control" rows="4" class="comment">${feature.properties.note}</textarea>
  </div>
  <div class="d-flex">
    <button type="submit" class="btn btn-outline-info btn-sm">Save</button>
    <button class="delete-button btn btn-outline-danger btn-sm ml-auto">Delete</button>
  </div>
</form>

设置弹出内容的代码

var points = new L.geoJson(null, {

  onEachFeature: function (feature, layer) {
    layer.bindPopup(feature.properties.note);

    let myPopup = L.DomUtil.create('div', 'content');

    content = `
    <form class="popup-form">  
      <div class="form-group">
        <label class="mb-0" for="comment">Comment:</label>
        <textarea class="form-control" rows="4" class="comment">${feature.properties.id}</textarea>
      </div>
      <div class="d-flex">  
        <button type="submit" class="btn btn-outline-info btn-sm">Save</button>
        <button class="delete-button btn btn-outline-danger btn-sm ml-auto">Delete</button>
      </div>
    </form>
    `;

    layer.bindPopup(content); // Create empty popup

    $('#form', myPopup).on('click', function() {
      alert("form clicked")
  });

受此帖子启发how to catch the click event on a leaflet popup

我不明白这个代码示例的“上下文”是什么?

var content = L.DomUtil.create('div', 'content'),
    popup = L.popup().setContent(content);

L.DomEvent.addListener(content, 'click', function(event){
    // do stuff
}, context);

【问题讨论】:

  • 您到底想访问哪个元素?表单代码是否放置在弹出窗口内?
  • 我添加了我正在使用的代码。我的目标是获取删除按钮上的单击事件(删除..)以及保存按钮以向数据库发出更新请求。我正在尝试在更新时手动添加侦听器

标签: jquery html css leaflet popup


【解决方案1】:

为了能够访问删除按钮类,一种方法是在 layer.bindPopup(popupContent) 上使用 on("popupopen") event

使用这种方式就不需要使用leaflet的L.DomUtil。 在收听popupopen event 时,您可以收听jquery 的click event,使用删除按钮的类分别从删除按钮调用删除事件。使用preventDefault 避免页面刷新。

function onEachFeature(feature, layer) {
  const popupContent = `
    <form class="popup-form">  
      <div class="form-group">
        <label class="mb-0" for="comment">Comment:</label>
        <textarea class="form-control" rows="4" class="comment">${
          feature.properties.id
        }</textarea>
      </div>
      <div class="d-flex">  
        <button class="btn btn-outline-info btn-sm">Save</button>
        <button class="delete-button btn btn-outline-danger btn-sm ml-auto">
           Delete
        </button>
      </div>
    </form>
    `;

  if (feature.properties && feature.properties.popupContent) {
    popupContent += feature.properties.popupContent;
  }

  layer.bindPopup(popupContent).on("popupopen", () => {
    $(".delete-button").on("click", e => {
      e.preventDefault();
      alert(`now delete layer with id ${feature.properties.id}`);
    });
  });
}

Demo

【讨论】:

  • 这太好了,谢谢!这就像我需要的那样工作:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-16
相关资源
最近更新 更多