我對此代碼有問題。即使我有兩個或多個部分,我也無法通過單擊「刪除按鈕」刪除某個部分。重複部分在刪除時失敗
任何人都可以看到錯誤是什麼?
// Add a new repeating section
var attrs = ['for', 'id', 'name'];
function resetAttributeNames(section) {
var tags = section.find('input, label'), idx = section.index();
tags.each(function() {
var $this = $(this);
$.each(attrs, function(i, attr) {
var attr_val = $this.attr(attr);
if (attr_val) {
$this.attr(attr, attr_val.replace(/_\d+$/, '_'+(idx + 1)))
}
})
})
}
$('.addFight').click(function(e){
e.preventDefault();
var lastRepeatingGroup = $('.repeatingSection').last();
var cloned = lastRepeatingGroup.clone(true)
cloned.insertAfter(lastRepeatingGroup);
resetAttributeNames(cloned)
});
// Delete a repeating section
$('.deleteFight').click(function(e){
e.preventDefault();
var current_fight = $(this).parent('div');
var other_fights = current_fight.siblings('.repeatingSection');
if (other_fights.length === 0) {
alert("You should atleast have one item");
return;
}
current_fight.slideUp('slow', function() {
current_fight.remove();
// reset fight indexes
other_fights.each(function() {
resetAttributeNames($(this));
})
})
});
這裏是我的HTML
<div class="repeatingSection">
<section class="panel">
<header class="panel-heading">
Item information
<div class="pull-right"><a href="#" class="btn btn-danger btn-sm deleteFight"><i class="fa fa-trash-o"></i>Remove</a></div>
</header>
<div class="panel-body">
<div class="form">
<div class="form-group ">
<label for="itemnumber_1" class="control-label col-lg-2">Itemnumber</label>
<div class="col-lg-10">
<input class=" form-control" id="itemnumber_1" name="itemnumber_1" type="text" />
</div>
</div>
<div class="form-group ">
<label for="quantity_1" class="control-label col-lg-2">Quantity</label>
<div class="col-lg-10">
<input class=" form-control" id="quantity_1" name="quantity_1" type="text" />
</div>
</div>
<div class="form-group ">
<label for="reason_1" class="control-label col-lg-2">Reason for return</label>
<div class="col-lg-10">
<textarea class=" form-control" id="reason_1" name="reason_1"></textarea>
</div>
</div>
</div>
</div>
</section>
</div>
<section class="panel">
<div class="panel-body">
<div class="form">
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<a href="#" class="btn btn-info addFight"><i class="fa fa-plus"></i> One more item</a>
</div>
</div>
</div>
</div>
</section>
您可以請創建jsfiddle – Mitul