我想使用的代碼從這個jsfiddle到更多的領域動態加載到窗體:上唯一的場動態添加輸入字段有一個按鈕,無需刪除按鈕
$('.multi-field-wrapper').each(function() {
var $wrapper = $('.multi-fields', this);
$(".add-field", $(this)).click(function(e) {
$('.multi-field:first-child', $wrapper).clone(true).appendTo($wrapper).find('input').val('').focus();
});
$('.multi-field .remove-field', $wrapper).click(function() {
if ($('.multi-field', $wrapper).length > 1)
$(this).parent('.multi-field').remove();
});
});
一切都很正常,但問題在於只有一個字段時,「刪除」按鈕將保留在那裏。
我想讓它看起來像this當只有一個字段。 javascript使用clone
函數。如果我刪除刪除按鈕,則會在添加更多字段時將其從所有後續「克隆」中刪除。我對JavaScript很陌生。當只有一個字段實例時,是否有人有關於如何刪除「刪除」按鈕的建議?
瞭解有關[事件委託](http://learn.jquery.com/events/event-委派/)方法。 –
[點擊事件對動態生成的元素不起作用]的可能重複](http://stackoverflow.com/questions/6658752/click-event-doesnt-work-on-dynamically-generated-elements) –
@BhojendraNepal問題是不是「刪除」按鈕不起作用,它只是沒有必要在頁面上唯一的字段 –