1 回答
TA贡献1815条经验 获得超13个赞
您可以创建一些函数,每次单击删除按钮时都会调用该函数。在此函数中,您需要遍历所有表,留下第一个表,然后用于添加.find()新值,即:count到显示的那个 td 1,2..etc。此外,您需要使用更改表的 ID$(this).attr("id", "timeTable" + newvalue);
演示代码:
var tableCount = 1;
var index = 1;
$(document).on('click', 'button.add_time', function(e) {
e.preventDefault();
tableCount++;
$('#timeTable').clone().attr('id', "timeTable" + tableCount).appendTo('#table');
$('#timeTable' + tableCount).find("input").val("");
index++;
$('#timeTable' + tableCount + ' .aa').html(tableCount);
});
$(document).on('click', 'button.removeTime', function() {
var closestTable = $(this).closest('table');
if (closestTable.attr('id') != "timeTable") {
closestTable.remove();
}
tableCount--;
resetValues(); //call to reset values
if (tableCount < 1) {
tableCount = 1;
}
return false;
});
function resetValues() {
counter = 2; //initialze to 2 because 1 is fixed
//looping through table not first one
$(".tg:not(:first)").each(function() {
//find .aa class replace its counter
$(this).find('.aa').text(counter);
$(this).attr('id', "timeTable" + counter);
counter++;
})
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="table" class="form-group">
<table id="timeTable" class="tg">
<tr class="form-group">
<td class="aa">1</td>
<td class="tg-yw4">
<button class="btn form-control btn-danger removeTime">Remove Events</button>
</td>
<td class="col-sm-4">
<input placeholder="Event Date" name="events[]" class="input-lg" type="text" onfocus="(this.type='date')">
</td>
</tr>
<tr>
<td class="aa">1</td>
<td class="tg-yw4">Event Description:</td>
<td>
<input name="event_descriptions[]" type="text" placeholder="Event description:" />
</td>
</tr>
</table>
</div>
<div class="my-5">
<button class="add_time btn btn-info">Add More Events</button>
</div>
添加回答
举报