2011-08-01 6 views
1

add 링크를 통해 새 입력 필드를 추가 할 수있는 양식이 있습니다. 새로 추가 된 각 입력 필드는 remove 링크를 통해 제거 할 수도 있습니다.동적 양식 - DOM 조작

add and remove 링크가 마지막에 추가 된 입력 필드에만 표시되도록하려면 어떻게해야합니까?

예 : jsfiddle - my code

$(function() { 
    $('a.add_input').live('click', function(event) { 
     event.preventDefault(); 
     var $class = '.' + $(this).closest('div.find_input').find('div').attr('class'); 
     var newDiv = $($class).find('div:first').clone(); 
     //alert(newDiv)     
     newDiv.append('<a href="" class="remove_input">remove</a>') 
     newDiv.find('input').each(function() { 
      $(this).val(''); 
     }); 
     $($class + ':first div:last').before(newDiv); 
     $(this).prev('a.add_input').remove(); 
     alert(ok) 
    }); 

    $('a.remove_input').live('click', function(event) { 
     event.preventDefault(); 
     $(this).closest('div').remove(); 
    }); 
}); 
+0

. 자유롭게 롤백하거나 편집 할 수 있습니다. –

답변

2

제외한 모든 컬렉션에서 마지막 .add_input을 제거

이해하는 문제를 쉽게 만들려고
$(this).closest('.find_input').find('.add_input:not(:last)').remove(); 
+0

아니요, 작동하지 않습니다. –

+0

편집에 실수를했습니다 ... 'add'링크는 마지막 요소에만 표시되어야합니다 ... 혼란스러워서 죄송합니다 : -/ –

+0

문제가 있습니다. http://jsfiddle.net/ 4BnHy/2/ –