2012-04-13 6 views
0

私の要素をクローンすると、私のデモは本当に明確です divを複製して削除しますが、最後の要素(li)は削除してはいけません。 私は行方不明ですか?jquery clone functionネスト削除オプション

この!:

$(document).on("click", 'li.delete',function() { 
      $(this).closest(".outerDiv").remove(); 
    if ($(this).is(".outerDiv:last")){ 
      return false; 
    } 

DEMOとは何かである:= 0 http://jsfiddle.net/XeELs/86/

jQueryの のvar cloneCount。 。 $( "#アドオンアドレス")をクリックします(関数(){

$("#to-add-address").clone() 
     .attr("id", "to-add-address_Clone" + cloneCount) 
     .insertAfter("#to-add-address"); 
    $("#clone", "#to-add-address_Clone" + cloneCount) 
     .attr("id", "clone_Clone" + cloneCount); 
    cloneCount++; 
}); 

$(document).on("click", '.options li a',function() { 
      $(this).closest(".options").find('li a').removeClass('selected'); 
      $(this).addClass('selected'); 

     }); 
$(document).on("click", 'li.delete',function() { 
      $(this).closest(".outerDiv").remove(); 
    if ($(this).is(".outerDiv:last")){ 
      return false; 
    } 

});

答えて

1

このコード

var cloneCount = 0; 
$("#add-address").click(function() { 

    $("#to-add-address").clone() 
     .attr("id", "to-add-address_Clone" + cloneCount) 
     .insertAfter("#to-add-address").addClass('cloned'); //add a new class cloned to the cloned outerDivs 
    $("#clone", "#to-add-address_Clone" + cloneCount) 
     .attr("id", "clone_Clone" + cloneCount); 
    cloneCount++; 
}); 

$(document).on("click", '.options li a',function() { 
      $(this).closest(".options").find('li a').removeClass('selected'); 
      $(this).addClass('selected'); 

     }); 
$(document).on("click", 'li.delete',function() { 
      $(this).closest(".outerDiv").filter('.cloned').remove(); // and delete only the cloned ones 
    if ($(this).is(".outerDiv:last")){ 
      return false; 
    } 
}); 
を試してみてください
1

があるどのように多くのアドレスブロックを見つけるためにsize()メソッドを使用し、これは一つだけ左があるまで、あなたがそれらを削除できるようになる:

$(document).on("click", 'li.delete',function() { 

    if ($('.outerDiv').size() > 1){ 
      $(this).closest(".outerDiv").remove(); 
    } 

} 

http://jsfiddle.net/2mby5/

関連する問題