我有一个输入,通过onclick函数,新的输入通过innerhtml添加,但是当我删除一个对象时,它就从列表中消失了。如何操作请帮助
var x = 0;
function addInputIIN() {
if (x < 4 ){
var profile = document.getElementById('formDriverInputs');
var div = document.createElement('div');
idX = 'driverIdInput' + [x + 1]
NameX = 'driverNameInput' + [x + 1]
div.id = 'inputDriverBlock' + ++x;
const numDriver = x + 1
div.innerHTML = '<div class="inputContainer d-flex align-items-start"><div class="input-group- prepend"><div class="input-group-text">'+numDriver+'</div></div><input type="text" class="form-control form__iin mb-3 position-relative" id='+ idX +' name='+ NameX +' aria-describedby="driver" placeholder="Введите иин" ><img src="https://w7.pngwing.com/pngs/185/104/png-transparent-emblem-icon-icons-matt-minus-symbol.png" alt="" class="form__img-add" alt="" onclick="delInputIIN()"> <a href=""> </div>';
console.log(div)
profile.appendChild(div);
console.log(x)
}else{
var toastBody = document.querySelector('.toast-body')
toastBody.innerHTML = 'Не может быть больше 5 водителей на 1 машину'
BsAlert.show();
}
}
function delInputIIN() {
var inputs = document.querySelectorAll('.inputIcon')
var div = document.getElementById('input' + x);
var parent = event.target.parentNode.parentNode
parent.remove();
--x
console.log(x)
}
.form__img-add{
width:32px;
}
<div class="form-group text-start" id="formDriverInputs">
<div id="inputDriverBlock0">
<div class="inputContainer">
<div class="input-group-prepend">
<div class="input-group-text">1</div>
<input type="text" id="driverIdInput0" name="driverNameInput0"placeholder="Введите иин" '> <img src="https://image.emojipng.com/610/1872610.jpg" alt="" class="form__img-add" onclick="addInputIIN()">
</div>
</div>
</div>
</div>
如何确保删除所有编号时按1到5的顺序进行
1条答案
按热度按时间cyvaqqii1#
如果你委托和克隆你会有一个更容易的时间
注意我是如何隐藏第一个减号的