我改变了以前的回答。现在,我添加了一个隐藏的输入,并在单击“添加行”和“删除”时更改了其值。然后当您单击保存按钮时,最后一条操作消息将被提醒。
检查下面的代码。
var isnewrowadded;
var isrowdeleted;
jQuery(document).ready(function() {
var id = 0;
jQuery("#addrow").click(function() {
id++;
var row = jQuery('.samplerow tr').clone(true);
row.find("input:text").val("");
row.attr('id', id);
row.appendTo('#dynamicTable1');
$('#change-status').val('New Row Added');
return false;
});
$('.remove').on("click", function() {
$(this).parents("tr").remove();
$('#change-status').val('Row Deleted');
});
$(document).on("click", "#btnSave", function() {
alert($('#change-status').val());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btnSave">Save</button>
<table id="dynamicTable1">
<thead>
<th>Id</th>
<th>Name</th>
<th>Age</th>
<th>Place</th>
<th>Skill</th>
</thead>
<tbody class="row-cont">
<tr id="0">
<td><input type="text" id="fld1" /></td>
<td><input type="text" id="fld2" /></td>
<td><input type="text" id="fld3" /></td>
<td><input type="text" id="fld4" /></td>
<td><input type="text" id="fld5" /></td>
<td><button class="remove">Remove</button></td>
</tr>
</tbody>
</table>
<input type="button" id="addrow" value="Add New Row" />
<table class="samplerow" style="display:none">
<tr>
<td><input type="text" id="fld1" /></td>
<td><input type="text" id="fld2" /></td>
<td><input type="text" id="fld3" /></td>
<td><input type="text" id="fld4" /></td>
<td><input type="text" id="fld5" /></td>
<td><button class="remove">Remove</button></td>
</tr>
</table>
<input type="hidden" id="change-status" value="No Change">