我正在尝试通过按钮将新字段添加到我的表单中。但是,它将它添加到表单的外部。关于编辑什么来实现这一点的任何想法?谢谢。
我的 HTML:
<div id="page">
<h1>Quick Order</h1>
<div id="dynamicInput">
<form method="post" name="QuickOrderMulti">
<input type="hidden" name="formName" value="dmiformQuickOrderMulti">
<p>Product # <input type='Text' name="ProductNumber" title="Enter Product #"></p>
<p>Product # <input type='Text' name="ProductNumber" title="Enter Product #"></p>
<input type="submit" value="Add"><input type="button" value="Add another text input" onClick="addInput('dynamicInput');">
</form>
</div>
</div> <!-- page -->
我的 JavaScript:
<script type="text/javascript">
var counter = 0;
var limit = 3;
function addInput(dynamicInput){
if (counter == limit) {
alert("You have reached the limit of adding " + counter + " inputs");
}
else {
var newdiv = document.createElement('div');
newdiv.innerHTML = "Product # " + (counter + 1) + " <br><input type='text' name='ProductNumber[]'>";
document.getElementById(dynamicInput).appendChild(newdiv);
counter++;
}
}
</script>