Это лучше, если вы можете поделиться HTML фрагмент кода, то легче построить код JS, во всяком случае здесь иллюстрация для вас:
Html (скажем все они созданы процессом петли)
// put class name on button for references
// 1st group
<input type="text" class="a" value="a"><button class="removeBtn">Remove</button>
<input type="text" class="a" value="b"><button class="removeBtn">Remove</button>
<input type="text" class="a" value="b"><button class="removeBtn">Remove</button>
// 2nd group
// this code wrapped inside parent container
<hr/>
<div class="container">
<input type="text" class="a" value="a" />
<button class="removeBtn2">Remove</button>
</div>
<div class="container">
<input type="text" class="a" value="b" />
<button class="removeBtn2">Remove</button>
</div>
<div class="container">
<input type="text" class="a" value="c" />
<button class="removeBtn2">Remove</button>
</div>
JQuery
// for first group of textbox
$(document).on('click', '.removeBtn', function() {
// Must be noted that, textbox must be aside with button
// that why we asked for HTML snippet
$(this).prev().remove().end().remove();
// or $(this).prev('.a').remove().end().remove();
});
// for second group of textbox
$(document).on('click', '.removeBtn2', function() {
// just remove it parents
$(this).closest('.container').remove();
});
DEMO
вы можете вставить HTML-код также – shafiq
http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example –
Нет необходимости создавать обработчик щелчка для каждой кнопки вы, может залить некоторый общий способ зарегистрировать обработчик кликов и удалить текстовое поле. Пожалуйста, поделитесь своим html-кодом –