Leaflet Popup Form:传单弹出表单提交中包括传单latlong值

时间:2018-08-31 19:18:40

标签: javascript jquery ruby-on-rails-4 leaflet leaflet.draw

我有一个使用Leaflet的弹出表单,并且希望将eventLatLong值包括在提交的参数中。 因此,我绘制了一个多边形或放下一个标记,弹出窗口应以用户输入名称的形式出现。该名称必须与绘图事件坐标保持一致。

这是我到目前为止所用的代码,花了数小时试图弄清楚。一般来说,我对诸如JQuery,Ajax和JS之类的前端东西非常陌生,因此不胜感激。

<script>
map.on(L.Draw.Event.CREATED, function (e) {
    eventLatLong = e.layer._latlng;
    var polyEvent = drawnItems.addLayer(e.layer);
    var popupForm = '<form class="simple_form business_form" id="form" novalidate="novalidate" enctype="multipart/form-data" action="/create_section" accept-charset="UTF-8" data-remote="true" method="post" _lpchecked="1"><input name="utf8" type="hidden" value="✓">\n' +
        '    <h2>\n' +
        '<input id="coords" type="hidden" value="">\n' +
        '        <div class="input string required thing_part_name"><label class="string required control-label" for="thing_part_name" id="name"><abbr title="required">*</abbr> Name</label><input class="string required" autofocus="autofocus" type="text" name="thing_part[name]" id="thing_part_name"></div>\n' +
        '        <div class="form-actions">\n' +
        '            <input type="submit" name="commit" value="Add New Poly">\n' +
        '        </div>\n' +
        '        <br>\n' +
        '    </h2>\n' +
        '</form>';
    polyEvent.bindPopup(popupForm, {
        keepInView: true,
        closeButton: true
    }).openPopup();

    $("#form").submit(function (e) {
        e.preventDefault();
    });
});
</script>

1 个答案:

答案 0 :(得分:0)

我知道了。

我要做的就是通过jquery的theData函数传递val值。

<script>
map.on(L.Draw.Event.CREATED, function (e) {

    var polyEvent = drawnItems.addLayer(e.layer);

    theBounds = JSON.stringify(e.layer.toGeoJSON())

    var popupForm = '<%= j render "path/to/form" %>';

    polyEvent.bindPopup(popupForm, {
        keepInView: true,
        closeButton: true
    }).openPopup();

    $('#bounds').val(theBounds);

    $("#form").submit(function (e) {
        e.preventDefault();
    });
});
</script>