简体   繁体   English

为什么我不能使用jQuery删除这些div?

[英]Why can't I remove these divs with jQuery?

I have dynamically created elements within a parent element of .output which are created within my on('click') function. 我在.output的父元素中动态创建了元素,这些元素是在on('click')函数中创建的。

$(document).on("click", '#search', function() {   
//Loop crime data array from api request above...
for(var i = 0; i < mapData.length; i++) {

  //Fill in crime data
  var fill = '<div class="row resu"><div class="col-xs-2 number"><h5>'+[i+1]+'</h5></div><div class="col-xs-10"><p class="text-primary">Type of crime: <span class="text-strong">'+mapData[i][0]+'</span></p><p class="text-primary">Location specifics: <span class="text-strong">'+mapData[i][1]+'</span></p></div></div>';

  //add to div
  $('.output').append(fill);  
}
...
.......

But nowhere outside the function am I able to target the new .resu rows for which I can apply .empty() or .remove() . 但是在函数之外的任何地方,我都无法定位可以应用.empty().remove()的新.resu行。 Every time I hit the search button I wish to empty the output container div first. 每次点击搜索按钮时,我希望先清空输出容器div。

If you go to my pen and hit the button, you will see some information regarding crime in that postcode's area. 如果您去找我的笔并按下按钮,您将在该邮政编码区域内看到一些有关犯罪的信息。 If you keep pressing it then multiple duplications of the same information will fill it up. 如果您持续按下它,则相同信息的多次重复将填满它。 How can I get rid of those with jquery? 我如何摆脱那些与jQuery的?

My pen: https://codepen.io/anon/pen/xrwXYy 我的笔: https : //codepen.io/anon/pen/xrwXYy

Here is the update DEMO: https://codepen.io/anon/pen/PjPJaZ 这是更新的DEMO: https : //codepen.io/anon/pen/PjPJaZ

The isuue is, You are adding data to mapData array after each request. 问题是,您将在每个请求之后将数据添加到mapData数组。 You need to reset the mapData array after each request as well. 您还需要在每个请求之后重置mapData数组。

function getPolApi(year,month,lat,lng,mapData) {
    $.ajax({
    url : "https://data.police.uk/api/crimes-at-location?date="+year+"-"+month+"&lat="+lat+"&lng="+lng,
    type : "get",
    async: false,
    success : function(data) {
     mapData = [];// Empty the array
      for(var i = 1; i < data.length; i++) {      
            mapData.push([
              data[i]['category'],
              data[i]['location']['street']['name'],
            ]);

           heatmapData.push(new google.maps.LatLng(data[i].location.latitude,data[i].location.longitude));
      }
       //Add results into view

          for(var i = 0; i < mapData.length; i++) {
             var fill = '<div class="row resu"><div class="col-xs-2 number"><h5>'+[i+1]+'</h5></div><div class="col-xs-10"><p class="text-primary">Type of crime: <span class="text-strong">'+mapData[i][0]+'</span></p><p class="text-primary">Location specifics: <span class="text-strong">'+mapData[i][1]+'</span></p></div></div>';
            $('.output').append(fill);

          }//endforloop

          //Move the map into view of the heatmaps
          moveMap(lat,lng);
          //Add the heatmaps
          addHeatMap();
      mapData = [];
      // console.log(mapData);
    },
    error: function(dat) {
       console.log('error');
    }

If you try to invoke 'getCords' asynchronously, you will find that the #output is actually be cleared before being replenished. 如果尝试异步调用“ getCords”,则会发现在实际补充输出之前已清除了#output So there must be something wrong with your getCords or getPolApi . 因此,您的getCordsgetPolApi必须存在问题。

setTimeout(function(){
    getCords(postcode);
    getPolApi(year,month,lat,lng,mapData);
},3000);

You could do 你可以做

 $('.output').html(fill);

instead of 代替

 $('.output').append(fill);  

In that case there is no need to remove resu 在这种情况下,无需删除resu

You are doing right with output div. 您正在正确处理output div。

But you missed out the mapData variable to be empty. 但是您错过了mapData变量为空。

Each time its getting push in your ajax success response for loop. 每次它推动您的ajax成功响应循环。

So you need to clear mapData veriable. 所以您需要清除mapData

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM