2013-07-18 4 views
0

이상한 이유로 인해 추가는 제거 만하는 것이 아니라 추가하는 것입니다. 여기 추가/제거가 필요하지 않습니다.

는 HTML입니다 :

<div class="insert-links"></div> 
<a href="#" id="button" onclick="return false;"> 
<img src="http://a.dryicons.com/images/icon_sets/blue_extended_icons_set/png/64x64/add.png" "> 
</a> 

와 jQuery를은 다음과 같다 :

<script type='text/javascript'>//<![CDATA[ 
$(function() { 
    var i = 0; 

    $('#button').click(function() { 
     if (i < 10) { 
      $('.insert-links').append('<p style="display: none;" class="new-link"><input tabindex="1" placeholder="Command eg: give #user# 264" style="width:285px\;margin-top: 12px\;" type="text" name="fname" id="fname"/> <input tabindex="1" placeholder="Price" style="width:45px\;background-color:#f4faff\;" title="Price in points of this item" type="text" name="fname" id="fname"/><a href="#" id="buttonremove" onclick="return false;"><img src="http://c.dryicons.com/images/icon_sets/blue_extended_icons_set/png/128x128/remove.png" style="width: 20px;float: right;margin-top: 19px;margin-right: 19px;"></a></p>'); 
      $('.insert-links').find(".new-link:last").slideDown("slow"); 
      i++; 
     } 
    }); 

    $('#buttonremove').click(function() { 
     if (i > 0) { 
      $('#buttonremove').parent('p').remove(); 
      i--; 
     } 
    }); 
});//]]> 
</script> 

사람이 좀 도와 주 시겠어요? 이것에 대한

답변

2

사용 jQuery를 Event Delegation는 :

Fiddle Demo

$('.insert-links').on('click', '#buttonremove', function() { 
    if (i > 0) { 
     $('#buttonremove').parent('p').remove(); 
     i--; 
    } 
}); 
+0

정말 고마워요. 이렇게하면 많은 시간을 절약 할 수 있습니다. 정말 감사합니다. – Farouqxii

0

문제는 특정 순간에 단락이 포함 당신이 페이지를로드하는 "buttonremove"에 클릭 이벤트를 바인딩한다는 것이다 다른 모든 요소는 아직 존재하지 않습니다. #button을 클릭하여 바인딩 코드를 이동하면됩니다.

+0

그 말이 맞습니다. 설명해 주셔서 대단히 감사합니다! – Farouqxii

관련 문제