将数据加载到html无效

时间:2016-11-29 11:35:55

标签: javascript html ajax leaflet

我有两个页面(html& js),我有从ajax(从数据库)返回的数据,我对它做了空间查询,我想在html页面的输入(列表框)中加载它的一些数据但是它不起作用(列表框中没有加载数据)。 这是html的一部分:

<div>
<select name="FarmersID" id="FarmersID" multiple style="width:172px">   
</select>
</div>

这是ajax部分

$.ajax({
        type:"POST",
        url:"CustomerID_geojson.php",
        data:{'OrdersID': Order_ID} ,
        dataType: 'json',
        success: function (response) { 
           var unit = 'kilometers';
           var buffered = turf.buffer(response, distance, unit);
           bufresult = buffered.geometry.coordinates;
           $.ajax({
                type: "POST",
                url: 'allfarmers_geojson.php',
                dataType: 'json',
                success: function (data) 
                {
                    var searchWithin = {
                      "type": "FeatureCollection",
                      "features": [
                        {
                          "type": "Feature",
                          "properties": {},
                          "geometry": {
                            "type": "Polygon",
                            "coordinates": bufresult
                          }
                        }
                      ]
                    };

                    var ptsWithin = turf.within(data,searchWithin);
                    //to check if there are any farmers within the distance or not
                    if (ptsWithin.features.length > 0)
                    {
                        geojsonLayer = L.geoJson(ptsWithin,
                            {
                                onEachFeature: function (feature, layer) 
                                {
                                    FarmerID=feature.properties.Farm_id;
                                    document.getElementById('FarmersID').value=FarmerID; ///This one is not working
                                    layer.bindPopup('<label>Farmer Name:</label>' + feature.properties.nick_name_ + '<br><label>Farmer ID:</label>' + feature.properties.Farm_id);
                                }
                            }).addTo(mymap);
                        mymap.fitBounds(geojsonLayer.getBounds());

                    }
                    else
                    {
                        alert("No Farmers Found!");
                    }
                }
            });

        }
    }); 

1 个答案:

答案 0 :(得分:1)

$('#FarmersID').append($('<option>', {
    value: 1,
    text: 'My option'
}));

reffer this anwer

我希望你能通过ajax响应为你的选择项添加选项。