我正在创建订单表单,我是新编程人员。这个想法是,客户可以添加新箱子来订购具有数量的新产品。使用此代码我可以创建一个包含产品的包装盒,但是我没有在产品包装箱旁放置一个包装盒。我想为产品创建一个,为数量创建另一个,当客户点击新产品创建新产品列表和数量列表或数量字段以输入数值时。 我尝试复制document.create ...并更改div,但我不知道如何指向另一个选择。我很感谢你们能帮助我。谢谢。使用Javascript中的选项添加输入类型选项
的JavaScript
var choices = [
"Product1",
"Product2",
"Product3"];
function addInput(divName) {
var newDiv = document.createElement('div');
var selectHTML = "";
selectHTML="<select>";
for(i = 0; i < choices.length; i = i + 1) {
selectHTML += "<option value='" + choices[i] + "'>" + choices[i] + "</option>";}
selectHTML += "</select>";
newDiv.innerHTML = selectHTML;
document.getElementById(divName).appendChild(newDiv);
var Div = document.createElement('div');
var selectHTML = "";
selectHTML="<select>";
for(i = 0; i < choices.length; i = i + 1) {
selectHTML += "<option value='" + choices[i] + "'>" + choices[i] + "</option>";}
selectHTML += "</select>";
Div.innerHTML = selectHTML;
document.getElementById(divName).appendChild(Div);
HTML
<form class="new" method="post" action="phppage">
<fieldset id="products"> <legend> PRODUCTS </legend>
<select name="tProduct" id="cProduct">
<optgroup label="GALLON BAG">
<option>Product1</option>
<option>Product2</option>
<option>Product3</option>
</optgroup>
</select>
<label for="cQuantity">Quantity:<span style="color:red">*</span></label> <input type="number" name="tQuantity" id="cQuantity" placeholder="Qnt" min="0" max="9999" required/>
<div id="dynamicInput"></div>
<input type="button" value="New Product" onclick="addInput('dynamicInput');" />
我试过这种方式,但没有工作,所以我改变了一些东西,并完美地工作。非常感谢你,因为我使用了你的想法。这解决了我的问题。 – Evandro
我不知道,当我把事件处理程序放在按钮标记中时,它不起作用。所以我把它们放在脚本中,所以你现在可以在这里试试这个demo ...哈哈。你应该试试jquery,那太棒了。 –