2013-11-26 26 views
0
<div class="add">Click me to add new item to list</div> 
<ul id="sortable"> 
    <li class="delete"> 
     <div class="item">these old items can be removed by click on <span class="delBtn">DEL</span></div> 
    </li> 
    <li class="delete"> 
     <div class="item">these old items can be removed by click on <span class="delBtn">DEL</span></div> 
    </li> 
    <li class="delete"> 
     <div class="item">these old items can be removed by click on <span class="delBtn">DEL</span></div> 
    </li> 
</ul> 


$("#sortable").sortable(); 

$(".add").click(function() { 
    var newItem = '<li class="delete"><div class="item">these <span class="red">new items cannot be removed</span> by click on <span class="delBtn">DEL</span></div></li>'; 
    $("#sortable").append(newItem); 
}); 

$("#sortable").delegate('.delBtn', 'click', function() { 
    alert("A click happened, it was captured at #commonParent and this alert ran");  
}); 

,而不是警告新加入的項目,我woud想刪除列表項... 請幫幫我,我該怎麼做?無法刪除從一個jQuery排序列表2.0

可以try it here ...

回答

0

嘗試.on代替:

$("#sortable").sortable(); 

$(".add").click(function() { 
    var newItem = '<li class="delete"><div class="item">these <span class="red">new items cannot be removed</span> by click on <span class="delBtn">DEL</span></div></li>'; 
    $("#sortable").append(newItem); 
}); 

$("#sortable").on('click','.delBtn', function() { 
    $(this).parent().remove(); 
}); 

小提琴:http://jsfiddle.net/MvnEv/111/

+0

THX!這是worx ...(我以前曾嘗試過......但顯然我做了錯誤的事情......現在它正在工作!) – Ruky

0

好,如果你正在使用jquery1.6 +,而是使用在委託..

$("#sortable").on('click','.delBtn', function() { 
    alert("A click happened, it was captured at #commonParent and this alert ran"); 
    $(this).parents('.delete').remove(); 
    //or 
    $(this).closest('.delete').remove(); 
}); 

remove()從DOM中刪除。

parents()closest()選擇器選擇li元素

fiddle here

+0

對不起......這種解決方案不僅能消除 「這個」,指的 DEL :-( – Ruky

+0

以及..我已經更新了我的answer..i認爲您在更新答案時看到了它。現在試試.. :) – bipen

+0

THX!你的新答案是好的,這個工作:-) THX再次!這是一個偉大的社區... – Ruky

0

你可以做這樣的事情

$("#sortable").sortable(); 

$(".add").click(function() { 
    var newItem = '<li class="delete"><div class="item">these <span class="red">new items cannot be removed</span> by click on <span class="delBtn">DEL</span></div></li>'; 
    $("#sortable").append(newItem); 
}); 

$("#sortable").delegate('.delBtn', 'click', function() { 
    $(this).closest("div").remove(); 
}); 

fiddle