2013-05-05 116 views
-1

我不知道這個問題的確切標題。 但我要問..點擊刪除元素

<div id="listInserts"> 
    <div>tes <a class="del" rel="54" href="#">X</a></div> 
</div> 

我有這樣

$.get('dashboard/xhrGetListings', function(o) { 

    for (var i = 0; i < o.length; i++) 
    { 
     $('#listInserts').append('<p>' + o[i].text + '<a class="del" rel="' + o[i].id + '" href="#">X</a></p>'); 
    } 

    $('#listInserts').on("click",".del",function() { 
     delItem = $(this); 
     var id = $(this).attr('rel'); 

     $.post('dashboard/xhrDeleteListing', {'id': id}, function(o) { 
      delItem.parent().remove(); 
     }, 'json'); 
     return false; 
    }); 
}, 'json'); 

上述腳本一個jQuery腳本只能部分地對我。我希望當點擊選擇器.del的集合中的某個項目時,id=listInserts上的可用數據將被刪除而無需重新加載頁面。該項目被刪除很好,但頁面正在重新加載。

回答

1

,如果你想防止頁面重新加載,使用preventDefault(),像

$('#listInserts').on("click",".del",function(evt) { 
    evt.preventDefault(); //prevents page from being reloaded 
    ..... 
+0

謝謝你sudhir已經回答了我的問題 ,但它仍然不起作用。 – 2013-05-05 06:21:59

+0

@IrvanKristian如果你想得到一個務實/更好的答案,在像http://jsfiddle.net這樣的網站上提供演示可以幫助很多。順便說一句,您的意思是「可用數據」_? – undefined 2013-05-05 06:24:26

+0

喜歡這個視頻.. 但在視頻中使用jquery 1.6 http://www.youtube.com/watch?v=4hh2IXrdT4g&list=PL7A20112CF84B2229&index=4 – 2013-05-05 06:26:02

0
  • 你的代碼不能與我
  • 重裝,但嘗試後「的」處理您的GET事件

    //get 
    $.get('dashboard/xhrGetListings', function(o) { 
    for (var i = 0; i < o.length; i++) 
    { 
        $('#listInserts').append('<p>' + o[i].text + '<a class="del" rel="' + o[i].id + '" href="#">X</a></p>'); 
    } 
    }, 'json'); 
    
    //on 
    $('#listInserts').on("click",".del",function() { 
    delItem = $(this); 
    var id = $(this).attr('rel'); 
    
    $.post('dashboard/xhrDeleteListing', {'id': id}, function(o) { 
        delItem.parent().remove(); 
    }, 'json'); 
    return false; 
    });