2 回答
TA贡献1806条经验 获得超5个赞
我相信你走在正确的道路上。看看这是否更接近您的需要。
var countBox = 2;
var boxName = 0;
function addInput()
{
var boxLabel = "<label>Company " + countBox + "</label>";
var element = document.getElementById('my-content');
var html = '<div class="row">'+boxLabel+': <input type="text" id="'+boxName+'" value="'+boxName+'" " /></div>';
appendHtml(element, html);
countBox += 1;
boxName += 1;
}
function appendHtml(el, str) {
var div = document.createElement('div');
div.innerHTML = str;
while (div.children.length > 0) {
el.appendChild(div.children[0]);
}
}
<input type="button" onclick="addInput()" value="Add new">
<div id="my-content" class="content">
</div>
我相信这更接近您的需要?
TA贡献1802条经验 获得超6个赞
您使用了正确的东西并且有点封闭...阅读代码并在有不清楚的地方添加评论。希望能帮助到你
let myButton = document.querySelector('#add-those-inputs');
myButton.addEventListener('click', event => { addInput(); });
let myPlaceholder = document.querySelector('.my-placeholder');
let addInput = function() {
let companyInputCounter = document.querySelectorAll('.my-company-input');
let inputWrapper = document.createElement('div');
let input = document.createElement('input');
input.classList.add('my-company-input');
input.type = 'text';
input.value = 'Company_' + companyInputCounter.length;
input.id = 'Company_' + companyInputCounter.length;
inputWrapper.appendChild(input);
myPlaceholder.appendChild(inputWrapper);
};
<input id="add-those-inputs" type="button" value="Click me to add more">
<div class="my-placeholder"></div>
- 2 回答
- 0 关注
- 105 浏览
添加回答
举报