I tried using document.createElement, which works fine to add textbox but I can't increment ID name. I am using it in a metro app.
我尝试使用document.createElement,它可以正常添加文本框,但我不能增加ID名称。我在地铁应用程序中使用它。
2 个解决方案
#1
0
function textBox(noOfInputTag){
noOfInputTag = noOfInputTag*1; // Convert to Number
//noOfInputTag is used to get total textbox you want to create
for(var i = 0; i <= noOfInputTag; i++){
var textbox = document.createElement('input');
textbox.type = 'text';`enter code here`
textbox.id = 'textBox_' + i; // As number of textbox increases id get incremented
document.body.appendChild(textbox);
}
}
textBox(2);
#2
0
<form>
No. of lines:
<input type="text" id="number" value="0"/>
<br/>
<input type="button" id="button" value="Add Line" />
</form>
<div>
<ol id="linedata">
</ol>
</div>
document.getElementById('button').addEventListener("click", incrementValue);
function incrementValue() {
var value = parseInt(document.getElementById('number').value, 10);
value = isNaN(value) ? 0 : value;
value++;
document.getElementById('number').value = value;
var list = document.createElement('li');
var box = document.createElement('input');
box.type = 'text';
box.id = 'line' + value;
box.placeholder='Line ' + value;
list.appendChild(box);
document.getElementById("linedata").appendChild(list);
}
#1
0
function textBox(noOfInputTag){
noOfInputTag = noOfInputTag*1; // Convert to Number
//noOfInputTag is used to get total textbox you want to create
for(var i = 0; i <= noOfInputTag; i++){
var textbox = document.createElement('input');
textbox.type = 'text';`enter code here`
textbox.id = 'textBox_' + i; // As number of textbox increases id get incremented
document.body.appendChild(textbox);
}
}
textBox(2);
#2
0
<form>
No. of lines:
<input type="text" id="number" value="0"/>
<br/>
<input type="button" id="button" value="Add Line" />
</form>
<div>
<ol id="linedata">
</ol>
</div>
document.getElementById('button').addEventListener("click", incrementValue);
function incrementValue() {
var value = parseInt(document.getElementById('number').value, 10);
value = isNaN(value) ? 0 : value;
value++;
document.getElementById('number').value = value;
var list = document.createElement('li');
var box = document.createElement('input');
box.type = 'text';
box.id = 'line' + value;
box.placeholder='Line ' + value;
list.appendChild(box);
document.getElementById("linedata").appendChild(list);
}