当我添加一个文本框时,主文本框自动输入已添加的文本框数量并不是完全需要的。错误:自动计算添加的文本框不应该是
这里是你传递ID作为参数代码
function addElement() {
var ni = document.getElementById('myDiv');
var numi = document.getElementById('theValue');
var num = (document.getElementById('theValue').value - 1 + 2);
numi.value = num;
var newdiv = document.createElement('div');
var divIdName = 'my' + num + 'Div';
newdiv.setAttribute('id', divIdName);
newdiv.innerHTML = '<div class="form-group"> <label for="color" class="control-label col-xs-4"><p class="left"></p></label> <div class="col-xs-7"> <input type=text id=' + num + 'value= ' + num + ' class= "req"><a href="javascript:remove(' + divIdName + ')">Remove</a>';
ni.appendChild(newdiv);
}
function remove(dId) {
var ni = document.getElementById('myDiv');
ni.removeChild(dId);
}
<label for="color" class="control-label col-xs-4">
<p class="left">Color/s</p>
</label>
<input name="color" class="req" id="theValue" autocomplete="off" required/>
<p><a class="btn btn-default bt " role="button" href="javascript:addElement()">Add Color</a>
</p>
<div id="myDiv"></div>
<div style='clear: both;'></div>
'(的document.getElementById( 'theValue')值 - 1 + 2);'为什么不使用号码()或parseInt函数()将其转换为数字? – epascarello
'Remove' – epascarello
@epascarello你能告诉我如何?我会在哪里把Remove – Vista