I am using jQuery
and JavaScript
to add/remove HTML
Element. in that case i remove specific html element
我正在使用jQuery和JavaScript来添加/删除HTML元素。在这种情况下,我删除特定的HTML元素
following code
$(document).ready(function() {
var i = 1;
$("#plus").click(function() {
i++;
$('#editrow').append("+<tr id=rownum_" + i + "><td>" + i + "</td>" +
"<td><input type='text' id='cid_" + i + "' class='form-control input-sm' pattern='[0-9]{1-10}' required></td>" +
"<td><input type='text' id='lac_" + i + "' class='form-control input-sm' pattern='[0-9]{1-10}' required></td>" +
"<td><input type='text' id='mnc_" + i + "' class='form-control input-sm' pattern='[0-9]{1-10}' required></td>" +
"<td><input type='text' id='mcc_" + i + "' class='form-control input-sm' pattern='[0-9]{1-10}' required></td>" +
"<td><button type='button' id=" + i + " onClick='delrow(" + i + ")' class='btn btn-warning btn-sm'>-</button></td></tr>");
});
});
function delrow(num) {
var element = document.getElementById("rownum_" + num);
element.parentNode.removeChild(element);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-condensed">
<thead>
<tr>
<th>#</th>
<th>CID</th>
<th>LAC</th>
<th>MNC</th>
<th>MCC</th>
<th align="right"><button id="plus" type="button" class="btn btn-primary btn-sm">+</span></button></th>
</tr>
</thead>
<tbody id="editrow">
<tr id=rownum_1>
<td>1</td>
<td><input type='text' id='cid_1' class='form-control input-sm' pattern='[0-9]{1-10}' required></td>
<td><input type='text' id='lac_1' class='form-control input-sm' pattern='[0-9]{1-10}' required></td>
<td><input type='text' id='mnc_1' class='form-control input-sm' pattern='[0-9]{1-10}' required></td>
<td><input type='text' id='mcc_1' class='form-control input-sm' pattern='[0-9]{1-10}' required></td>
<td><button type='button' id=1 onClick='delrow(1)' class='btn btn-warning btn-sm'>-</button></td>
</tr>
</tbody>
</table>
Using this code i add element like index 1,2,3,4,5,6 and i remove some element index like 3,4,5 ,and that will deleted. after we add any element they started with 7,8,9 and show index like 1,2,6,7,8,9 now total element is 6. how to manage that ? , i want to get that index like 1,2,3,4,5,6
使用此代码我添加像索引1,2,3,4,5,6这样的元素,并删除一些像3,4,5这样的元素索引,这将被删除。在我们添加任何元素后,他们开始使用7,8,9并显示索引,如1,2,6,7,8,9现在总元素是6.如何管理? ,我想得到像1,2,3,4,5,6这样的指数
1 个解决方案
#1
1
I think the best way to handle inserts and updates to the table would be to keep a separate function resetRowIndex
to assign the row number to every tr
, without worrying about the index at the time of insertion or deletion. The function resetRowIndex
can be called after every insert or delete to reset the index of every record.
我认为处理表的插入和更新的最佳方法是保持一个单独的函数resetRowIndex,将行号分配给每个tr,而不必担心插入或删除时的索引。每次插入或删除后都可以调用函数resetRowIndex来重置每条记录的索引。
$(document).ready(function() {
resetRowIndex();
$("#plus").click(function() {
var i = 0;
$('#editrow').append("+<tr id=rownum_" + i + "><td>" + i + "</td>" +
"<td><input type='text' id='cid_" + i + "' class='form-control input-sm' pattern='[0-9]{1-10}' required></td>" +
"<td><input type='text' id='lac_" + i + "' class='form-control input-sm' pattern='[0-9]{1-10}' required></td>" +
"<td><input type='text' id='mnc_" + i + "' class='form-control input-sm' pattern='[0-9]{1-10}' required></td>" +
"<td><input type='text' id='mcc_" + i + "' class='form-control input-sm' pattern='[0-9]{1-10}' required></td>" +
"<td><button type='button' id=" + i + " onClick='delrow(event)' class='btn btn-warning btn-sm'><span class='glyphicon glyphicon-remove-sign'>Remove</span></button></td></tr>");
resetRowIndex();
});
});
function delrow(event) {
var element = $(event.target).closest("tr");
element.remove();
resetRowIndex();
}
function resetRowIndex() {
var idx = 0;
$("#editrow tr").each(function() {
$(this).find("td").first().text($(this).index() + 1);
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-condensed">
<thead>
<tr>
<th>#</th>
<th>CID</th>
<th>LAC</th>
<th>MNC</th>
<th>MCC</th>
<th align="right"><button id="plus" type="button" class="btn btn-primary btn-sm"><span class="glyphicon glyphicon-plus-sign">Add</span></button></th>
</tr>
</thead>
<tbody id="editrow">
<tr id=rownum_1>
<td>1</td>
<td><input type='text' id='cid_1' class='form-control input-sm' pattern='[0-9]{1-10}' required></td>
<td><input type='text' id='lac_1' class='form-control input-sm' pattern='[0-9]{1-10}' required></td>
<td><input type='text' id='mnc_1' class='form-control input-sm' pattern='[0-9]{1-10}' required></td>
<td><input type='text' id='mcc_1' class='form-control input-sm' pattern='[0-9]{1-10}' required></td>
<td><button type='button' id=1 onClick='delrow(event)' class='btn btn-warning btn-sm'><span class='glyphicon glyphicon-remove-sign'>Remove</span></button></td>
</tr>
</tbody>
</table>
Although this is not the most optimal way of doing so, it is probably the simplest. If you intend to make this optimal, you can write some logic to keep track of the numbers getting deleted, and update subsequent records etc.
虽然这不是最佳方式,但它可能是最简单的方法。如果您打算使其达到最优,您可以编写一些逻辑来跟踪被删除的数字,并更新后续记录等。
#1
1
I think the best way to handle inserts and updates to the table would be to keep a separate function resetRowIndex
to assign the row number to every tr
, without worrying about the index at the time of insertion or deletion. The function resetRowIndex
can be called after every insert or delete to reset the index of every record.
我认为处理表的插入和更新的最佳方法是保持一个单独的函数resetRowIndex,将行号分配给每个tr,而不必担心插入或删除时的索引。每次插入或删除后都可以调用函数resetRowIndex来重置每条记录的索引。
$(document).ready(function() {
resetRowIndex();
$("#plus").click(function() {
var i = 0;
$('#editrow').append("+<tr id=rownum_" + i + "><td>" + i + "</td>" +
"<td><input type='text' id='cid_" + i + "' class='form-control input-sm' pattern='[0-9]{1-10}' required></td>" +
"<td><input type='text' id='lac_" + i + "' class='form-control input-sm' pattern='[0-9]{1-10}' required></td>" +
"<td><input type='text' id='mnc_" + i + "' class='form-control input-sm' pattern='[0-9]{1-10}' required></td>" +
"<td><input type='text' id='mcc_" + i + "' class='form-control input-sm' pattern='[0-9]{1-10}' required></td>" +
"<td><button type='button' id=" + i + " onClick='delrow(event)' class='btn btn-warning btn-sm'><span class='glyphicon glyphicon-remove-sign'>Remove</span></button></td></tr>");
resetRowIndex();
});
});
function delrow(event) {
var element = $(event.target).closest("tr");
element.remove();
resetRowIndex();
}
function resetRowIndex() {
var idx = 0;
$("#editrow tr").each(function() {
$(this).find("td").first().text($(this).index() + 1);
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-condensed">
<thead>
<tr>
<th>#</th>
<th>CID</th>
<th>LAC</th>
<th>MNC</th>
<th>MCC</th>
<th align="right"><button id="plus" type="button" class="btn btn-primary btn-sm"><span class="glyphicon glyphicon-plus-sign">Add</span></button></th>
</tr>
</thead>
<tbody id="editrow">
<tr id=rownum_1>
<td>1</td>
<td><input type='text' id='cid_1' class='form-control input-sm' pattern='[0-9]{1-10}' required></td>
<td><input type='text' id='lac_1' class='form-control input-sm' pattern='[0-9]{1-10}' required></td>
<td><input type='text' id='mnc_1' class='form-control input-sm' pattern='[0-9]{1-10}' required></td>
<td><input type='text' id='mcc_1' class='form-control input-sm' pattern='[0-9]{1-10}' required></td>
<td><button type='button' id=1 onClick='delrow(event)' class='btn btn-warning btn-sm'><span class='glyphicon glyphicon-remove-sign'>Remove</span></button></td>
</tr>
</tbody>
</table>
Although this is not the most optimal way of doing so, it is probably the simplest. If you intend to make this optimal, you can write some logic to keep track of the numbers getting deleted, and update subsequent records etc.
虽然这不是最佳方式,但它可能是最简单的方法。如果您打算使其达到最优,您可以编写一些逻辑来跟踪被删除的数字,并更新后续记录等。